-
selection sort
I have to create a random array of ints[]. Then I have to sort the array of ints using a selection sort. I cant seem to figure out what im doing wrong with my code. If anyone could help I would greatly appreciate it. Thanks
import java.util.*;
import java.io.*;
public class RandomInts
{
private static final int SIZE = 10;
Random ran = new Random();
int[] a = new int[SIZE];
public void random(){
for(int i = 1; i < a.length; i++){
a[i]= (int)(Math.random() * 100);
System.out.println(a[i]);
}
System.out.println("\n");
}
public void selectionSort() {
for (int i=0; i<a.length-1; i++) {
int min = i;
for (int j=i+1; j<a.length; j++) {
if (a[min] > a[j]) {
min = j;
}
}
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
System.out.println(a[min]);
}
}
}
public static void main(String[] args){
RandomInts b = new RandomInts();
b.random();
b.selectionSort();
}
}
That is the code I have so far and this is what it prints out:
30
54
50
83
28
20
23
59
91
30
54
50
83
83
83
It needs to print from largest to smallest smallest to largest.
thanks
-
nothing is wrong, you need to print the output in main() NOT inside the sorting loop.