Simple selection sorting

Source: Internet
Author: User

1. Algorithm Introduction

In the set of numbers to be sorted, select the minimum (or maximum) number to exchange with the 1th position, and then in the remaining number, find the minimum (or maximum) number of the 2nd position to exchange, and so on, until the first N-1 element (the penultimate number) and the nth element (the last a single number) is compared.

2. Algorithm principle

First trip, from N records to find the minimum key code record and the first record exchange;

Second, the second record from the beginning of the n-1 record to select the minimum key code record and the second record exchange;

And so on .....

On the first trip, the record with the smallest key code is selected from the N-i+1 record beginning with the first record, and the first record is exchanged,

Until the entire sequence is ordered by key code.

3. Source code

1 voidPrintintA[],intNinti) {  2cout<<"Section"<<i+1<<"Tour:"; 3      for(intj=0; j<8; J + +){  4COUT&LT;&LT;A[J] <<"  "; 5     }  6cout<<Endl; 7 }  8 /** 9 * Minimum value of the arrayTen  *  One * @return The key value of an int array A  */   - intSelectminkey (intA[],intNinti) - {   the     intK =i;  -      for(intj=i+1;j< N; ++j) { -         if(A[k] > a[j]) k =J;  -     }   +     returnK;  - }   +    A /**  at * Select Sort -  *  -  */   - voidSelectsort (intA[],intN) {   -     intkey, TMP;  -      for(inti =0; i< N; ++i) { inKey = Selectminkey (A, n,i);//Select the smallest element -         if(Key! =i) {   toTMP = A[i]; A[i] = A[key]; A[key] = tmp;//the smallest element is interchanged with the position I element +         }   - Print (A, n, i);  the     }   * }   $ intMain () {Panax Notoginseng     inta[8] = {3,1,5,7,2,4,9,6};  -cout<<"Initial value:";  the      for(intj=0; j<8; J + +){   +COUT&LT;&LT;A[J] <<"  ";  A     }   thecout<<endl<<Endl;  +Selectsort (A,8);  -Print (A,8,8);  $}

4. Algorithmic time complexity

O (n^2)

5. Stability analysis

Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move back to the second 5).

Simple selection sorting

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.