Sort (1)---------Select Sort (c language implementation)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.