Java Basics - Selection Sort

Each pass selected minimum (or maximum) element from a data element to be sorted,

It has been properly arranged in order of the last order, until all the data elements to be sorted SEQUENCE drained.

Select sort is unstable sorting method.

 Selection sort time complexity of O (n ^ 2).

        The first n elements to check, but then the number of elements in order to check for the n - 1, n - 2, ..., 2 and 1. Average number of elements of each inspection 1/2 * n, so running time n * 1/2 * n, writing simple O (n ^ 2).

Example:

34,4,56,17,90,65

The first set of five 4,34,56,17,90,65

The second round 4,17,56,34,90,65 4

4,17,34,56,90,65 the third round three times

4,17,34,56,65,90 fourth round twice

4,17,34,56,65,90 fifth round 1

code show as below:

ppublic static  void main (String [] args) {
         int [] = {34,4,56,17,90,65 NUM };
         int minIndex = 0; // for each comparison of the minimum recording subscripts
         // control rounds 
        for ( int I = 0; I <-num.length. 1; I ++ ) { 
            minIndex = I; // round assuming a minimum index 
            for ( int J = I +. 1; J <num.length; ++ J ) {
                 iF (NUM [minIndex]> NUM [J]) { 
                    minIndex = J; 
                } 
            } 
          // determined to be exchanged for themselves whether the subscripts
            if (minIndex! = i) { 
                num [minIndex] num [minIndex] + num [i]; 
                Surely, [i] = num [minIndex] - whether [i]; 
                Surely [minIndex] num [minIndex] - whether [i]; 
            } 
        } 
       // 输出结果
        for ( int x: num) 
        System.out.println (x); 
        
    }

 

Guess you like

Origin www.cnblogs.com/s1023/p/11183130.html