Select Sort is a very simple and very basic algorithm in all sorting algorithms, logic is simple, easy to implement, a lot of complex algorithms are from these simple algorithms evolved. Therefore, to learn the algorithm, we must first learn from these simple algorithms, and gradually, for the back to learn the complex algorithm lay the foundation. This article mainly from the "Basic principles, sequencing process, core code, algorithm performance, stability, reference code" and so on several aspects of the introduction of this algorithm.
Rationale: Find the smallest (or largest) element in the backlog, and swap the position with the first element of the sequence, then find the smallest (or largest) element from the remaining elements, and swap the position of the second element of the sequence ... And so on until the entire sequence is sorted.
Sorting process: The following sequence:5 3 0 4 1 9 7 2 6 8 For example, the red element represents the smallest element of the current sequence, and the bold element represents each element that participates in the comparison, and the non-bold element represents an already sorted element (not participating in the comparison).
0
1 5 4 3 9 7 2 6 8
Number of Trips |
Before sorting |
After sorting |
Description |
1 |
5 3 0 4 1 9 7 2 6 8 |
0 3 5 4 1 9 7 2 6 8 |
0 element minimum, swap position with first element 5 |
2 |
< Span style= "font-size:15px" >0 3 5 4 1 9 7 2 6 8 |
1 element minimum, swap position with second element 3 |
3 |
< Span style= "font-size:15px" >0 1 5 4 3 9 7 2 6 8 |
0 1 2 4 3 9 7 5 6 8 |
2 element min, with third element 5 interchange position |
4 |
< Span style= "font-size:15px" >0 1 2 4 3 9 7 5 6 8 |
0 1 2 3 4 9 7 5 6 8 |
3 element minimum, swap position with fourth element 4 |
5 |
< Span style= "font-size:15px" >0 1 2 3 4 9 7 5 6 8 |
0 1 2 3 4 9 7 5 6 8 |
4 element minimum, with Fifth Element 4 interchange position |
6 |
< Span style= "font-size:15px" >0 1 2 3 &NBSP 4 9 7 5 6 8 |
0 1 2 3 4 5 7 9 6 8 |
5 element minimum, swap position with sixth element 9 |
7 |
< Span style= "font-size:15px" >0 1 2 3 4 5 7 9 6 8 |
0 1 2 3 4 5 6 9 7 8 |
6 element minimum, with seventh element 7 interchange position |
8 |
< Span style= "font-size:15px" >0 1 2 3 4 5 6 9 7 8 |
0 1 2 3 4 5 6 7 9 8 |
7 element Minimum, Swap position | with eighth element 9
9 |
< Span style= "font-size:15px" >0 1 2 3 4 5 6 7 9 < Span style= "COLOR: #ff0000" >8 |
0 1 2 3 4 5 6 7 8 9 |
8 element minimum, with Nineth Element 9 interchange position |
10 |
< Span style= "font-size:15px" >0 1 2 3 4 5 6 7 8 9 |
0 1 2 3 4 5 6 7 8 9 |
9 element minimum, with tenth element 9 interchange position |
The final sort result is: 0 1 2 3 4 5 6 7 8 9.
Core code: Take Java as an example.
Public Static voidSortint[] a) {intlen = a.length;//Array Length for(inti = 0; i < Len; i++) {//The first for loop specifies the number of sort trips intmin = i;//minimum element position for(intj = i+1; J < Len; J + +) {//second for loop finds the smallest element of a sequence if(A[j]<a[min]) min = j;//if the J position element is less than the current minimum element, the J position is assigned to Min } intCha = a[i];//Exchange the minimum position element with the I position elementA[i] =A[min]; A[min]=cha; }}
algorithm Performance: The performance of the algorithm directly determines the execution efficiency of the algorithm, the measurement algorithm performance has some common indicators, such as time complexity, space complexity, while different coding, different machine hardware and other factors will affect the efficiency of the algorithm execution. for sequences of length N, choose the smallest element for each trip, and compare the size of the unsorted element, so the total number of comparisons is: (N-1) + (N-2) +......+2+1=n (N-1)/2; Each time you select the smallest element to exchange, you can schedule an element, So the total number of exchanges is: N. In the synthesis, the time complexity is O (N2), and the space complexity is O (1).
Stability: If there are two or more identical elements in the sequence, their relative order does not change after sorting (such as the existence of a sequence of NI=NJ, before the Order NI in NJ before, after the order NI is still in front of NJ), the sorting algorithm is stable, otherwise it is unstable. The selection sort is an unstable sorting algorithm, such as a sequence [2, 2,1], which becomes [1, 2,2], the first 2 (red) and 1 swap positions, two 2 relative position changes, So it's not stable.
In general, the choice of sorting algorithm is a very easy to understand and implement a simple sorting algorithm, its element movement is the least, only n Exchange, the number of exchanges and sequence size is linear relationship, the rest of the algorithm most of the growth order is linear logarithm or square level. Of course, selecting the sort algorithm each trip will only mechanically traverse the sequence to find the smallest element, if the sequence is already ordered or all elements are equal, it is obvious that such a traversal is superfluous, which is a major disadvantage in selecting a sort.
Reference code: Take Java as an example.
ImportJava.util.Random;/** Select Sort*/ Public classSelectionsort { Public Static voidSortint[] a) {intlen = a.length;//Array Length for(inti = 0; i < Len; i++) {//The first for loop specifies the number of sort trips intmin = i;//minimum element position for(intj = i+1; J < Len; J + +) {//second for loop finds the smallest element of a sequence if(A[j]<a[min]) min = j;//if the J position element is less than the current minimum element, the J position is assigned to Min } intCha = a[i];//Exchange the minimum position element with the I position elementA[i] =A[min]; A[min]=cha; } } Public Static voidMain (string[] args) {random random=NewRandom (); int[] Arg1 =New int[20]; for(intn=0;n<20;n++) {//generate 20 random numbers from [0-100]Arg1[n] = random.nextint (100); } System.out.println ("Before sorting:"); for(inti = 0; i < arg1.length; i++) {System.out.print (Arg1[i]+" "); } System.out.println ("\ n Sort after:"); LongStartTime = System.currenttimemillis ();//Get start timesort (arg1); LongEndTime = System.currenttimemillis ();//Get end Time for(inti = 0; i < arg1.length; i++) {System.out.print (Arg1[i]+" "); } System.out.println ("\ n Sort Duration:" + (Endtime-starttime) + "MS"); }}
Operation Result:
Pre-order:6, 6 19 29 31 33 37 38 44 48 52 54 59 63---- ---- The length of the order: 0ms
Select sorting algorithm