I've been working on this problem:
Create an array of 10 random integers, and array of 1,000 random integers, and an array of a million (1,000,000) random integers. For each array: Display the first ten elements of the array. Call selection sort for the array. Display the first ten elements of the sorted array.
Here's my code so far:
public class Lab_10_3 {
public static void main(String[] args) {
int myRandomArray1[] = new int[10];
int myRandomArray2[] = new int[1000];
int myRandomArray3[] = new int[1000000];
count(myRandomArray1);
printOriginal(myRandomArray1);
selectionSort(myRandomArray1);
printArray(myRandomArray1);
count(myRandomArray2);
printOriginal(myRandomArray2);
selectionSort(myRandomArray2);
printArray(myRandomArray2);
count(myRandomArray3);
printOriginal(myRandomArray3);
selectionSort(myRandomArray3);
printArray(myRandomArray3);
}
public static void count(int[] value) {
System.out.println("The original order is: ");
for (int i = 0; i < 10; i++) {
value[i] = (int) (Math.random() * 10);
}
}
public static void selectionSort(int[] array) {
int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (array.length - 1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for (index = startScan + 1; index < array.length; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
public static void printArray(int[] value) {
System.out.println("The sorted values are:");
for (int i = 0; i < 10; i++)
System.out.print(value[i] + " ");
System.out.println();
}
public static void printOriginal(int[] value) {
for (int j = 0; j < 10; j++) {
System.out.println(value[j]);
}
}
}
For some reason my randomArray3 (1,000,000) won't print it's sorted values, and I can't figure out why...(I'm still very new to Java, and I really want to know how and why this isn't working, so I can correct myself and learn.)
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire