1. Algorithm Introduction
In the set of numbers to be sorted, select the minimum (or maximum) number to exchange with the 1th position, and then in the remaining number, find the minimum (or maximum) number of the 2nd position to exchange, and so on, until the first N-1 element (the penultimate number) and the nth element (the last a single number) is compared.
2. Algorithm principle
First trip, from N records to find the minimum key code record and the first record exchange;
Second, the second record from the beginning of the n-1 record to select the minimum key code record and the second record exchange;
And so on .....
On the first trip, the record with the smallest key code is selected from the N-i+1 record beginning with the first record, and the first record is exchanged,
Until the entire sequence is ordered by key code.
3. Source code
1 voidPrintintA[],intNinti) { 2cout<<"Section"<<i+1<<"Tour:"; 3 for(intj=0; j<8; J + +){ 4COUT<<A[J] <<" "; 5 } 6cout<<Endl; 7 } 8 /** 9 * Minimum value of the arrayTen * One * @return The key value of an int array A */ - intSelectminkey (intA[],intNinti) - { the intK =i; - for(intj=i+1;j< N; ++j) { - if(A[k] > a[j]) k =J; - } + returnK; - } + A /** at * Select Sort - * - */ - voidSelectsort (intA[],intN) { - intkey, TMP; - for(inti =0; i< N; ++i) { inKey = Selectminkey (A, n,i);//Select the smallest element - if(Key! =i) { toTMP = A[i]; A[i] = A[key]; A[key] = tmp;//the smallest element is interchanged with the position I element + } - Print (A, n, i); the } * } $ intMain () {Panax Notoginseng inta[8] = {3,1,5,7,2,4,9,6}; -cout<<"Initial value:"; the for(intj=0; j<8; J + +){ +COUT<<A[J] <<" "; A } thecout<<endl<<Endl; +Selectsort (A,8); -Print (A,8,8); $}
4. Algorithmic time complexity
O (n^2)
5. Stability analysis
Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move back to the second 5).
Simple selection sorting