Data Structure-simple selection sort)
Selection sort ):In the n-I + 1 record, the records with the smallest keyword are selected as the I-th record in the sequence.
Simple selection sort ):By comparing the n-I keywords, the records with the minimum keyword are selected from the n-I + 1 record and exchanged with the I record.
Select sort needs to compare n (n-1)/2 times, namely (n-1) + (n-2) +... + 1 = [(n-1) + 1] (n-1)/2 times,The time complexity is O (n ^ 2).
Simple selection and sortingMain StepsYes: 1. Select the location of a small element. 2. Switch.
Code:
/* * SimpleSelectionSort.cpp * * Created on: 2014.6.5 * Author: Spike */#include
#include
void print(const std::vector
& L) {for (auto i : L) {std::cout << i << ;}std::cout << std::endl;}int SelectMinKey(std::vector
& L, const size_t p) {int min = p;for (size_t i=p+1; i
& L) {for (size_t i=0; i
L = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};SelectSort(L);print(L);}
Output:
4 38 65 97 76 13 27 49 55 49 4 13 65 97 76 38 27 49 55 49 4 13 27 97 76 38 65 49 55 49 4 13 27 38 76 97 65 49 55 49 4 13 27 38 49 97 65 76 55 49 4 13 27 38 49 49 65 76 55 97 4 13 27 38 49 49 55 76 65 97 4 13 27 38 49 49 55 65 76 97 4 13 27 38 49 49 55 65 76 97 4 13 27 38 49 49 55 65 76 97 4 13 27 38 49 49 55 65 76 97