Sorting Algorithm: Select sorting method

Source: Internet
Author: User

Select the sorting method: in the sorting array, select the minimum (or maximum) number to exchange with the number of 1st positions, and then find the minimum (or maximum) number among the remaining values) and so on until the nth element (second to last) and NTH element (last number) are compared.

Implementation ideas:

1. Find the minimum number (maximum number) each time );

2. Find the minimum number and the I-th array for the I-th;

3. Repeat (1) (2) until the sorting is successful;

 

  0 1 2 3 4 5 6 7
  6 10 4 90 52 77 8 3
0 3 10 4 90 52 77 8 6
1 3 4 10 90 52 77 8 6
2 3 4 6 90 52 77 8 10
3 3 4 6 8 52 77 90 10
4 3 4 6 8 10 77 90 52
5 3 4 6 8

10

52 90 77
6 3 4 6 8 10 52 77

90

 

 

 

 

 

 

 

 

 

 

Code implementation:

/*************************************** **************************************** * ******* Description: * input ARGs: * output ARGs: * return value: **************************************** **************************************** * ***/INT select_sort (INT array [], int N) {int I, j; int temp, little; for (I = 0; I <n-1; I ++) {/* Find the min one */little = I; for (j = I + 1; j <n; j ++) {If (array [little]> array [J]) // small to large little = J ;}
/* Exchange the postion if not the min one */If (little! = I) {temp = array [little]; array [little] = array [I]; array [I] = temp ;}} return 0 ;} /* ----- end of select_sort ()-----*/

 

 

Reference: http://blog.csdn.net/caryaliu/article/details/7438592

Sorting Algorithm: Select sorting method

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.