Select sort-Simple selection sort (Easy Selection sort)

Source: Internet
Author: User

Basic idea:

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.

Examples of simple selection sorting:

Operation Method:

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.


Algorithm implementation:

void print (int a[], int n, int i) {cout<< "section" <<i+1 << "Trip:"; for (int j= 0; j<8; J + +) {Cout<<a[j] << "  ";} Cout<<endl;} /** * The minimum value of the array * * @return The key value of the int array */int selectminkey (int a[], int n, int i) {int k = i;for (int j=i+1;j< n; ++j) {if (a[ K] > A[j]) k = j;} return k;} /** * Select sort * */void selectsort (int a[], int n) {int key, tmp;for (int i = 0; i< N; ++i) {key = Selectminkey (A, n,i);           Select the smallest element   recursive if (key! = i) {tmp = A[i];  A[i] = A[key]; A[key] = tmp; The smallest element swaps}print (A, n, i) with the first position element  ;}} int main () {int a[8] = {3,1,5,7,2,4,9,6};cout<< "initial value:"; for (int j= 0; j<8; j + +) {Cout<<a[j] << "  ";} Cout<<endl<<endl;selectsort (A, 8);p rint (a,8,8);}

Simple selection sorting improvement--two Yuan Select sort

Simple selection of sorting, each loop can only determine the positioning of an element after sorting. We can consider improving the position of two elements (the current maximum and minimum records) for each cycle, thus reducing the number of cycles required for sorting. Improved to sort n data, up to [N/2] loop. The specific implementation is as follows

void Selectsort (int r[],int n) {  .    int I, J, Min, Max, tmp;  For    (i=1; I <= n/2;i++) {    Geneva.        Do not exceed N/2 to select sort   .        min = i; max = i; Record the maximum and minimum key record locations separately  .        for (j= i+1; j<= n-i; j + +) {  .            if (R[j] > R[max]) {   .                max = J; Continue;   Continue, otherwise you may miss some data, min value is large.            }    Ten.            if (r[j]< R[min]) {one   .                min = j;   .      }      //The exchange operation can also be discussed to improve the efficiency      of the. TMP = r[i-1]; R[I-1] = R[min]; R[min] = tmp;  (      tmp = r[n-i]; R[n-i] = R[max]; R[max] = tmp;   .    }   19.}  

  

Select sort-Simple selection sort (Easy Selection sort)

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.