1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| // 选择排序 public static int [] selectSort(int arr[]){ int n = arr.length;
for(int i = 0; i<n - 1; i++){ int minIndex = i; for(int j = i+1; j< n; j++){ if(arr[j]<arr[minIndex]){ minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } return arr; } public static void main(String[] args) { int [] arrays = { 1, 3, 6,4,7,12,5,1,2}; int[] arraysSort = selectSort(arrays); System.out.println(Arrays.toString(arraysSort)); }
|