例子3.6 利用选择排序的算法实现对一维数组中的数据进行生序排序
1 public class SelectSort { 2 public static void main (String [] args) { 3 int [] arr = {23, 22, 345, 65, 111, 5, 656}; 4 sort(arr); 5 System.out.println("选择排序后: "); 6 for (int i = 0; i < arr.length; i ++) { 7 System.out.println(arr [i] + ","); 8 } 9 } 10 public static void sort (int [] arr ) { 11 int temp = 0; 12 int min = 0; 13 for (int i = 0; i < arr.length -1; i ++) { 14 min = i; 15 for (int j = i+ 1; j < arr.length; j ++) { 16 if (arr [min] > arr[j]) 17 min = j; 18 } 19 if (min != i) { 20 temp = arr [min]; 21 arr [min] = arr [i]; 22 arr [i] = temp; 23 } 24 } 25 } 26 }
原文:https://www.cnblogs.com/allison-aichipingguo/p/10586237.html