1 package log; 2 3 public class Test4 { 4 5 /** 6 * java算法---选择排序 7 * 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // 需要排序的数组 12 int arr[] = { 49, 20, 36, 51, 18, 94, 61, 31, 50 }; 13 // 循环输出该数组内容 14 System.out.println("排序之前:"); 15 for (int a : arr) { 16 System.out.print(a + " "); 17 } 18 System.out.println(); 19 20 int position = 0; 21 for (int i = 0; i < arr.length; i++) { 22 int j = i + 1; 23 position = i; 24 int temp = arr[i]; 25 for (; j < arr.length; j++) { 26 if (arr[j] < temp) { 27 temp = arr[j]; 28 position = j; 29 } 30 } 31 arr[position] = arr[i]; 32 arr[i] = temp; 33 } 34 // 循环输出该数组内容 35 System.out.println("排序之后:"); 36 for (int a : arr) { 37 System.out.print(a + " "); 38 } 39 System.out.println(); 40 41 } 42 43 }
下面这个是控制台输出