Select sort performance version

Two additional parameters, with the value of the num record array, index recording subscript.

Find the minimum exchange value after the start of the array, thus reducing the number of calls to the exchange function, a little bit to enhance performance

. 1  public  class Demo {
 2      public  static  void main (String [] args) {
 . 3          int [] = {54,1,23 ARR, -9,188,63 };
 . 4          printArry (ARR);
 . 5          SelectSort (ARR);
 . 6          printArry (ARR);
 . 7      }
 . 8      public  static  void the swap ( int [] ARR, int I, int J) { // switching elements in the array 
. 9          int TEMP = ARR [I];
 10          ARR [I] = ARR [J] ;
 11         arr[j]=temp;
12     }
13     public static void printArry(int[] arr) {//打印数组
14         System.out.print("[");
15         for(int i=0;i<arr.length;i++) {
16             if(i<arr.length-1)
17             System.out.print(arr[i]+",");
18             else
19                 System.out.println(arr[i]+"]");
20         }
21     }
22     public static void selectSort(int[] arr) {//选择排序性能版
23         for(int i=0;i<arr.length-1;i++) {
24             int num=arr[i];
25             int index=i;
26             for(int j=i+1;j<arr.length;j++) {
27                 if(num>arr[j]) {
28                     num=arr[j];
29                     index=j;
30                 }
31             }
32             if(i!=index)
33                 swap(arr,i,index);
34         }
35     }
36 }

 operation result:

Guess you like

Origin www.cnblogs.com/langdao/p/11029114.html