C + + selection sorting method

Source: Internet
Author: User

Select Sort:

It is known that a set of unordered data a[1], a[2] 、、、、 a[n], if it is sorted in ascending order.

The value of a[1] and a[2] is compared first, if A[1] is greater than a[2, the value of both is exchanged, otherwise it is unchanged.

Then compare the values of a[1] and a[3], and if A[1] is greater than a[3, the values of both are exchanged, otherwise they will not change.

Compare a[1] with a[4], and so on, and finally compare the values of a[1] and A[n].

After this process, the value of a[1] must be the smallest in this set of data.

The a[2] and a[3],a[4],,,,a[n] are compared in the same way, the value of a[2] must be the smallest of a[2] to a[n].

A[3] and a[4] to a[n] are compared in the same way, and so on.

A[1], a[2] 、、、、 A[n] are arranged in ascending order after the n-1 round is processed.

Features are:

(1) Advantages: stable, compared to bubble sort, the number of data movement is less than bubble sort;

(2) Disadvantages: relative or slow.

Specific code:

<span style= "FONT-SIZE:18PX;" ># include <stdio.h> main () {    int a[10],i,j,k,t,n=10;    printf ("Please enter 10 numbers:");   for (i=0;i<10;i++)     scanf ("%d", &a[i]);            /* Assign to array a[] *                  /for (i=0;i<n-1;i++)/     * Outer loop control number, n number of loops n-1 times, because do not need to compare size with yourself */         {     k=i;                           /* Assign the first element of the a[] array to K, assuming A[k] is an extremum *     /for (j=i+1;j<n;j++)/  * Find the most value from the next number to the last number *       /if (A[k]>a[j]) /      * If there is a greater than the maximum value of */                                k=j;                   /* mark it under K *     /if (k!=i)/             * If K is not the initial I value, the description is found later than its larger number */     {                            t=a[k];                             A[k]=a[i];                          a[i]=t;  }   /* Swaps the first number of values and the current sequence *    /} printf ("The sorted Numbers:");   for (i=0;i<10;i++)          printf ("%d   ", A[i]);   printf ("\ n"); }  </span>


C + + selection 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.