1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public static void main(String[] args) {
int[] arr = {234, 7567, 21, 33, 567, 643, 1, 1234, 89, 123}; int[] res = sort(arr); System.out.println("res = " + Arrays.toString(res)); }
public static int[] sort(int[] arr) {
if (arr == null || arr.length == 1) { return arr; } for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) { minIndex = arr[j] < arr[minIndex] ? j : minIndex; } System.out.println("min:" + minIndex); int tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; System.out.println("经过第"+i+"次循环数组为"+Arrays.toString(arr)); } return arr; }
|
能否参与评论,且看个人手段。