Choose the basic idea of sorting:
Select sort (Selection sort) is a simple and intuitive sorting algorithm.
It works like the following. First find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest (large) element from the remaining unsorted elements, and place it at the end of the sorted sequence.
And so on Until all elements are sorted.
My popular explanation:
① first time finds the smallest value in the entire sequence. To move it to the first place, the sequence is divided into 1,n-1;
② continues to find the smallest value in n-1, moving it to the first of the n-1, the second of the entire sequence
③ and so on, repeat the procedure ②, get the result
The main advantages of selecting a sort are related to data movement. Assuming that an element is at the correct last position, it will not be moved. Select sort each time a pair of elements is exchanged, at least one of them will be moved to its last position, so the table of n elements is sorted for a total of up to n-1 times.
In all of the sorting methods that rely entirely on swapping to move elements, choosing a sort is a good one.
Source:
Sort. CPP: Defines the entry point of the console application. #include "stdafx.h" #include <stdlib.h>void select_sort () {int arr[10];for (int i=0; i<10; i++) //Initialize data { Arr[i] = rand ()%30; Randomly generated data}printf ("before sort:\n"); Print the data before sorting for (int i = 0; i < i++) {printf ("%d", Arr[i]);} Start sort int min = 0; Assume that the minimum value under the cursor is 0for (int i = 0; i < 10-1; i++) {min = i;for (int j = i + 1; j <; J + +) {if (Arr[min] > Arr[j]) {min = j;}} if (min! = i) //assuming min = = I proves that the element of the first subscript in the second half of the current is the minimum value, no matter what the operation. Otherwise the interchange {int temp = Arr[min];arr[min] = arr[i];arr[i] = temp;}} printf ("\nafter sort:\n"); Print sorted data for (int i = 0; i < i++) {printf ("%d", Arr[i]);}} int _tmain (int argc, _tchar* argv[]) {select_sort ();p rintf ("\ n"); system ("pause"); return 0;}
Execution Result:
Before Sort:11 4 4 14After sort:4 4 10 11 14 17 18 18 22 29 Press the random key to continue ...
If there are errors, I hope to point out.
Sort (1)---------Select Sort (c language implementation)