package day02_code.heima.algorithm.sort.selection;
import java.util.Arrays;
public abstract class Selection {
public static void sort(Comparable[] a) {
for (int i = 0; i < a.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (greater(a[minIndex], a[j])) {
minIndex = j;
}
}
exchange(a, i, minIndex);
}
}
public static boolean greater(Comparable a, Comparable b) {
return a.compareTo(b) > 0;
}
public static void exchange(Comparable[] a, int i, int j) {
Comparable temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
Comparable[] a = {4, 5, 3, 2, 1};
Selection.sort(a);
System.out.println(Arrays.toString(a));
}
}