Selection sorting is one of the sorting algorithms, which is explained by the example of small to large sorting.
Basic ideas and illustrative examples
The basic idea of choosing a sort (from small to large) is to first select the smallest number, put it in the first position, then choose the second small number, put it in the second position, and so on, until all the numbers are sorted from small to large.
In implementation, we usually first determine the position of the small number of I, and then, it is exchanged with the number of the first.
Below, the sorting process is performed with a selection of 3 2 4 1, using Min_index to record the current minimum number of positions.
1th round sorting process (looking for the position of the 1th small number)
3 2 4 1 (originally, Min_index=1)
3 2 4 1 (3 > 2, so min_index=2)
3 2 4 1 (2 < 4, so min_index=2)
3 2 4 1 (2 > 1, so min_index=4, this time determined the 1th small number in position 4)
1 2 4 3 (1th round result, Exchange 3 and 1, i.e. position 1 and position 4 Exchange)
2nd round sorting process (looking for the position of the 2nd small number)
1 2 4 3 (1th round results, min_index=2, only need to start from position 2)
1 2 4 3 (4 > 2, so min_index=2)
1 2 4 3 (3 > 2, so min_index=2)
1 2 4 3 (2nd round result, because the min_index position is just in the 2nd position, no Exchange required)
3rd round sorting process (looking for the position of the 3rd small number)
1 2 4 3 (2nd round results, min_index=3, only need to start from position 2)
1 2 4 3 (4 > 3, so min_index=4)
1 2 3 4 (3rd round result, Exchange 3 and 4, i.e. position 4 and position 3 Exchange)
At this point, the sorting is complete.
Summary and realization
Select sort to sort an unordered array of size N r[n] and make a N-1 wheel selection process. The first wheel selects the small number of I and places it in the first position. When the first N-1 is completed, the number of the nth small (that is, the largest) is naturally in the final position.
#include <stdlib.h>#include<stdio.h>voidSelect_sort (intA[],intN//n number of elements for array a{ //make N-1 wheel selection for(intI=0; i<n-1; i++) { intMin_index =i; //find the location of the small number of I for(intj=i+1; j<n; J + +) { if(A[j] <A[min_index]) {Min_index=J; } } //Place the small number of I in the first position; if it happens, you don't have to exchange it. if(I! =Min_index) { inttemp =A[i]; A[i]=A[min_index]; A[min_index]=temp; } }} intMain () {inta[Ten]; intN; inti; scanf ("%d",&N); for(i=0; i<n;i++) {scanf ("%d",&A[i]); } select_sort (A, n); for(intI=0; I < n; i++) {printf ("%d", A[i]); } printf ("\ n"); return 0;}
Note: Selecting sort is an unstable sorting algorithm that may disrupt the original order of two identical numbers.
For example, sequence 5 8 5 2 9, in order from small to large, the first round will be the 1th number 5 will be exchanged with 2, then the original sequence of 2 5 of the relative sequence is destroyed, so the selection of the order is an unstable sorting algorithm.
Select Sort 3