Basic idea:
The initial sequence (a[0]~a[n-1]) is used as the sequence to be sorted, the first time to find the smallest element in the sequence to be sorted (a[0]~a[n-1]), and the first element in the sequence a[0], the sequence (A[0]) is ordered; the next pass is sorted in the subsequence to be sorted (a[1]~a[n-1 ]). Order I, a[i-1]~a[n-1], find the smallest element that is exchanged with the first element in the subsequence a[i-1]. The order of the initial sequence is ordered after n-1.
Simple Selection Sorting Example:
Code:
void Selectsort (int a[],int N)//simple select sort {int small;for (int i=0; i<n; i++) {small=i;for (int j=i+1; j<n; J + +) {if (a[j]& Lt A[small]) Small=j;} Swap (A[i],a[small]);}}
time complexity Analysis:
The algorithm is independent of the initial sequence arrangement. Regardless of the initial sequence, the algorithm must perform a n-1 trip, performing a comparison of n-i-1 sub-keywords per trip,
The total number of comparisons is:
Therefore, the best, worst, and average time complexity for simple selection sorting is O (n^2).
In addition, the sorting algorithm passes a sequencing to determine the final position of an element. Simple selection sorting is an unstable sorting algorithm.
Resources:
"Data structure" Chen Huinan People's post and telecommunications press
Simple selection of sorting algorithm