The core of the selection sort is: Select the smallest element and the header interchange per trip.
Time complexity: O (n^2).
Select Sort is an unstable sort, for what? because it is not good to handle the position of the two number of positions, for example, sequence 5 8 5 2 9, we know that the first time to select the 1th element 5 and 2 Exchange, then the original sequence of 2 5 of the relative sequence is destroyed, so the selection is not a stable sorting algorithm. So a stable sort is sure not to change the relationship between equal numbers before the position.
The implementation code is as follows:
#include <iostream>using namespace std; void Selectsort (int a[], int n) {for (int i = 0; i < n-1; i++) //Note n-1{int minindex = i+1; for (int j = i+1; J < N J + +) {if (A[j] < A[minindex]) Minindex = j; Select i+1 the smallest number}if (A[minindex] < a[i]) swap (A[minindex], a[i]); }}int Main () {int a[] = {1, 6, 4, 3, 8, 2}; Selectsort (A, 6); for (int i = 0; i < 6; i++) cout<<a[i]<< "; cout<<endl; return 0; }
Reference Links:
http://blog.csdn.net/xiazdong/article/details/8462393
http://blog.csdn.net/hguisu/article/details/7776068
Http://zhidao.baidu.com/link?url=zFcwJJZtDlBDx-5GK6_4EVHMseEJg_mWwNnPVAym4yPJk9YjYch3qpF_-FSyKspp_wxQZnS1UUzlpZxGhspHUK
Review data structure: Sort (iii)--select sort