[Principle] First, find the smallest (or largest) element in the unordered sequence, and store it in the starting position of the sequence. Then, find the smallest (or largest) element from the remaining unordered elements and put it at the end of the sorted sequence. And so on until all elements are sorted. [Complexity and stability]
(1) Select the sorting time complexity
The time complexity of sorting is O (n2 ).
Suppose there are n numbers in the sorted series. The time complexity of traversing a trip is O (n), and The N-1 needs to be traversed. Therefore, the time complexity of sorting is O (n2 ).
(2) Select sorting Stability
Sorting is a stable algorithm that satisfies the definition of a stable algorithm.
Algorithm stability -- assume that a [I] = A [J] exists in the sequence. If a [I] is before a [J], and after sorting, A [I] is still before a [J. The sorting algorithm is stable!
[Code]
# Include <iostream> using namespace STD; Template <class T> // use void selection_sort (T array [], const int size) for the template; int main () {int A [10]; cout <"Please input 10 numbers:" <Endl; For (INT I = 0; I <10; I ++) // input content {CIN> A [I];} selection_sort (A, sizeof (a)/sizeof (INT); // call the function, note the meaning of sizeof (a)/sizeof (INT) for (Int J = 0; j <10; j ++) // output Array {cout <A [J] <"" ;}return 0 ;}template <class T> void selection_sort (T array [], cons T int size) {int min; // determines the minimum value of the array subscript int temp; // the median value for (INT m = 0; m <size-1; m ++) // determine the boundary of the outer loop (that is, the maximum value of the loop) {min = m; // determine the subscript of the minimum value of the array and fix the sorted for (INT n = m + 1; n <size; n ++) // The inner loop is used to determine the minimum data {If (array [N] <array [Min]) // to determine the minimum value {min = N; // determine the minimum array subscript} If (Min! = M) // exchange data {temp = array [m]; array [m] = array [Min]; array [Min] = temp ;}}}