直接选择排序
public class SelectionSort {
public static void main(String[] args) {
int[] toBeSorted = { 1, 3, 5, 2, 4, 6 };
for (int i = 0; i < toBeSorted.length; i++) {
int minIndex = i;
for (int j = i; j < toBeSorted.length; j++) {
if (toBeSorted[j] < toBeSorted[minIndex]) {
minIndex = j;
}
}
int t = toBeSorted[minIndex];
toBeSorted[minIndex] = toBeSorted[i];
toBeSorted[i] = t;
}
for (int p = 0; p < toBeSorted.length; p++) {
System.out.print(toBeSorted[p]);
}
System.out.println();
}
}
间接选择排序:
public class IndirectInsertionSort {
public static void main(String[] args) {
int[] toBeSorted = { 1, 3, 5, 2, 4, 6, 0, -1 };
int[] sorted = new int[toBeSorted.length];
final int MAX_INT = 9999;
for (int f = 0, minIndex = 0; f < sorted.length; f++) {
for (int i = 0; i < toBeSorted.length; i++) {
if (toBeSorted[i] < toBeSorted[minIndex]) {
minIndex = i;
}
}
sorted[f] = toBeSorted[minIndex];
toBeSorted[minIndex] = MAX_INT;
}
for (int i = 0; i < toBeSorted.length; i++) {
System.out.print(sorted[i]);
}
}
}