Java-选择排序法
1.思路:按升序,每次都找到最小值的下标,然后与当前循环的道位交换
2.代码
1 public class TestArray { 2 3 public static void main(String[] args) { 4 int [] array = new int[args.length]; 5 for(int i = 0 ;i < args.length ;i++){ 6 array[i] = Integer.parseInt(args[i]); 7 } 8 9 System.out.println("排序前"); 10 for(int i : array){ 11 System.out.print(i+"\t"); 12 } 13 14 System.out.println("\n排序后"); 15 //Arrays.sort(array); 16 TestArray.selectedSort(array); 17 for(int i : array){ 18 System.out.print(i+"\t"); 19 } 20 } 21 22 public static void selectedSort(int [] array){ 23 for(int i = 0; i < array.length; i++){ 24 int index = i; 25 for(int j = i; j < array.length; j++){ 26 if(array[index] > array[j]){ 27 index = j; 28 } 29 } 30 if(i!=index){ 31 int temp = array[i]; 32 array[i] = array[index]; 33 array[index] = temp; 34 } 35 36 } 37 } 38 39 }
3.java TestArray 3 7 6 9 的运行结果为
You can do anything you set your mind to, man!