Sorting algorithm 6--Selection sort--Simple selection sorting

Source: Internet
Author: User

Simple selection sorting

Simple selection sort belongs to the selection sort, the idea of sorting is: Each trip from the records to be sorted to select the smallest record of the keyword, in order after the sequence of records sorted, until all the rows are complete.

1. Simple selection of the sorting method is to find the maximum value per cycle, the end of the loop to adjust the maximum value to the appropriate position, the number of exchanges less.

Each time you find the smallest element in the current unordered queue with the first swap location, select the second small one with the second swap location
Original queue: 3 5 6 2 4 1 (minimum element 1 and 3 interchange)
First step: 1 5 6 2 4 3 (current minimum element 2 and 5 interchange)
Step Two: 1 2 6 5 4 3 (Current minimum element 3 and 6 interchange)
Step Three: 1 2 3 5 4 6 (Current minimum element 4 and 5 interchange)
Fourth Step: 1 2 3 4 5 6
Fifth Step: 1 2 3 4 5 6
Sixth step: 1 2 3 4 5 6

2. Complexity of Time

Best case (positive order) does not move

Worst case (reverse order) Move 3 (n-1) times

Average Time complexity O (n*n)

Space complexity O (1)

For detailed analysis of time complexity, please refer to: http://www.cnblogs.com/zhangxue521/p/6748085.html

3. Algorithm Features

① Stable Sorting

② can be used for chained storage structures

③ Mobile records are less frequent, and when each record occupies more space, this method is faster than the direct insert sort

Java implementations:

1  Packageusually used;2 3 ImportJava.util.Scanner;4 5  Public class_2 Simple selection Sort {6      Public Static voidMain (string[] args) {7         intA[] =New int[6];8Scanner Scanner =NewScanner (system.in);9           for(inti = 0; i < a.length; i++) {TenA[i] =scanner.nextint (); One         } A               for(inti = 0; i < a.length; i++) { -                 intMin =i; - //loops through the smallest number in the current queue and assigns the Min flag to it the                  for(intJ =i+1; J < A.length; J + +) { -                     if(a[j]<A[min]) { -Min =J; -                     } +                 } -                 //Exchange minimum value to the front of the current unordered queue +                     if(min!=i) { A                         inttemp =A[i]; atA[i] =A[min]; -A[min] =temp; -                     } -                  for(intm:a) { -System.out.print (m+ ""); -                 } in System.out.println (); -             } to     } +}

JS implementation:

1 functionJiandansort (a) {2     vari,j,temp,min;3      for(i=0;i<a.length;i++) {4Min =i;5         //Loop to find the minimum value of the current queue6          for(j = i+1;j<a.length;j++) {7             if(a[j]<A[min])8Min =J;9         }Ten         //swap the minimum value to the front of the current unordered queue One         if(min!=i) { Atemp =A[i]; -A[i] =A[min]; -A[min] =temp; the         } -     } - } - varA =NewArray (7,2,6,5,1,4,3); + Jiandansort (a); -document.write ("_6 Simple selection Sort:" +a + "<br/>");

Sorting algorithm 6--Selection sort--Simple selection sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.