一、代码
package algorithm;
public class SelectionSort {
public static void selectionSort(int[] arr) {
if (null == arr || arr.length <= 0) {
return;
}
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
print(arr);
}
private static void print(int[] arr) {
for (int e : arr) {
System.out.print(e + " ");
}
}
public static void main(String[] args) {
int[] arr = {13, 7, 5, 8, 64, 2, 4, 62, 0, 1};
selectionSort(arr);
}
}
二、运行结果