Untitled
unknown
plain_text
a year ago
1.3 kB
14
Indexable
package assignment3;
import java.util.Scanner;
public class BubbleShort
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// prompt for length of array
System.out.println("How many numbers do you want to sort out?");
int length = in.nextInt ();
int count = 0;
// setting up array ; store values
int [] store = new int [length];
int [] unsortedArr = new int [length];
while (count < length)
{
System.out.println("What number would you like to sort?");
int values = in.nextInt ();
store[count] = values;
unsortedArr[count] = values;
count++;
}
// Sorting
int n = store.length;
for (int i = 0; i < n; i++) {
for (int j = 1; j < n-i; j++) {
if (store[j-1] > store[j]) {
int temp = store[j];
store[j] = store[j-1];
store[j-1] = temp;
}
}
}
// Output
System.out.print("Given values: ");
for (int i=0; i < store.length; i++) {
System.out.print(unsortedArr[i] + " ");
// arrays can't be system.out.print by itself (use i)
}
System.out.print("Sorted values: ");
for (int i=0;i < store.length; i++ ) {
System.out.print(store[i] + " ");
}
}
}
Editor is loading...
Leave a Comment