Sort - choose to sort

1. What is the selection sort?

Select sort is never ordered queue to find the smallest element to the smallest element on the head, and then continue to look for the smallest element from the rest of the unordered queue, on the back of the original elements of the sort

2, the principles of the code

  1. A total array size selection sort sorting wheel -1
  2. Each one sort interior is a loop, the loop rule:
    • Suppose that the current unsorted first element is a first minimum number
    • And then the back of each number are compared, if there are less than the current number of the number of re determine the minimum number, and subscript give
    • When traversing to the end of the array, you get the minimum number of round and subscript
    • Exchange code and then continue back to a new round of sorting

3, the progressive realization of the code:

// need sorted array 
int [] = {101,34,119,1 ARR};

Seen from the above principle, the array required 3 (arr.length-1) Sort wheel

// first round 
int minIndex = 0;    // assumed minimum number is the index of the first element of the array 
int min = ARR [minIndex]; // minimum number of values 
for ( int I = 0 +. 1; I <ARR .length; I ++) { // begin to circulate from the rear comparing the minimum number of 
            iF (min> ARR [I]) {   // when encountered is smaller than the minimum setting number 
                minIndex = I;    // the minimum value the new index is moved to the minimum value found 
                min = ARR [I];    // current record this minimum value 
            } 
        } 
        IF (! minIndex = 0) {     // than 0 indicates no later than a preset minimum is found smaller, so do not change position 
            ARR [minIndex] = ARR [0 ]; 
            ARR [ 0] =min; 

        } 
        System.out.println ( "first round:" + Arrays.toString (arr)) ;
        // second round 
        minIndex =. 1; // assumed minimum index of the first element remaining unsorted subscript 
        min = ARR [minIndex];   
         for ( int I = +. 1. 1; I <arr.length; I ++ ) {
             IF (min> ARR [I]) { 
                minIndex = I; 
                min = ARR [I]; 
            } 
        } 
        IF (minIndex =. 1! ) { 
            ARR [minIndex] = ARR [. 1 ]; 
            ARR [ . 1] = min ; 

        } 
        System.out.println ( "second round:" + Arrays.toString (arr))     ;
// third round 
        minIndex = 2; // assumed minimum index of the first element remaining unsorted subscript 
        min = ARR [minIndex];   
         for ( int I = +. 1. 1; I <arr.length; I ++ ) {
             IF (min> ARR [I]) { 
                minIndex = I; 
                min = ARR [I]; 
            } 
        } 
        IF (minIndex = 2! ) { 
            ARR [minIndex] = ARR [2 ]; 
            ARR [ 2] = min ; 

        } 
        System.out.println ( "third round:" + Arrays.toString (arr))     ;

The above has completed the ordering, the above three code comparison, there are many similarities, then merged

for ( int j = 0; j <arr.length-1; j ++ ) { 

            int minIndex = j;
            int I = arr [minIndex];
            for ( int i = 1 + j; i <arr.length; i ++ ) {
                 if (I> arr [i]) { 
                    minIndex = i; 
                    I = arr [i]; 
                } 
            } 
            If (minIndex! = J) { 
                arr [minIndex] = arr [j]; 
                arr [j] = min; 
            } 
        }
        System.out.println ( "Sort after completion of:" + Arrays.toString (arr)) ;

4, the time complexity

Because sorting uses for two cycles, so that O (n ^ 2), but the time complexity when compared to the bubble sort , the sort round is to find the smallest unit then moves to the head, less than the operand bubble sort operation execution time than the bubble sort faster

Guess you like

Origin www.cnblogs.com/han200113/p/11708045.html