1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class SelectSort { public static void main(String[] args) { int[] arr = {5, 9, 1, 6};
for (int i = 0; i < arr.length-1; i++) { int min = arr[i]; int minindex = i;
for (int j = i + 1; j < arr.length; j++) { if (min > arr[j]) { min = arr[j]; minindex = j; } } if (arr[i] > min) { arr[minindex] = arr[i]; arr[i] = min; }
System.out.println(Arrays.toString(arr)); }
} }
|