选择排序:每次遍历未排序过的数组中选择出最小值,并于已排序的下一个元素交换。时间复杂度为O(n^2)。
import java.util.Arrays;
public class Main {
public static void sort(int[] arr){
for (int i = 0; i < arr.length-1; i++) {
int min_index = i;
for (int j = i+1; j < arr.length; j++) {
min_index = arr[min_index] < arr[j] ? min_index : j;
}
int tem = arr[i];
arr[i] = arr[min_index];
arr[min_index] = tem;
}
}
public static void main(String[] args) {
int[] arr = {9,4,1,2,3,8,6,7,5};
sort(arr);
System.out.println(Arrays.toString(arr));
}
}