Selection sort of basic algorithm

Source: Internet
Author: User

The idea of sorting is simple, and it works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning of the sequence until all the data elements are sorted out.

The choice of sorting is easier to implement, but less efficient, O (N2).

The C language code is implemented as follows:

1 //Select a sort of C language implementation2 voidSelection_sort (inta[])3 {4     intI, J;5     intMin =0;6      for(i=0; i<maxsize-1; i++)7     {8          for(j=i; j<maxsize; J + +)9         {Ten             if(A[j] <=A[min]) { OneMin =J; A             } -         } - swap (A, I, min); the     } -}

A simple test case:

#include <stdio.h>#defineMAXSIZE 10voidSelection_sort (inta[]);voidSwapintA[],intIintj);//Select a sort of C language implementationvoidSelection_sort (inta[]) {    intI, J; intMin =0;  for(i=0; i<maxsize-1; i++)    {         for(j=i; j<maxsize; J + +)        {            if(A[j] <=A[min]) {min=J;    }} swap (A, I, min); }}voidSwapintA[],intIintj) {    inttemp =A[i]; A[i]=A[j]; A[J]=temp;}intMain () {intA[maxsize]; inti; printf ("Please input the num:\n");  for(i=0; i<maxsize; i++) {scanf ("%d",&A[i]); } printf ("before the sort:\n");  for(i=0; i<maxsize; i++) {printf ("%d", A[i]); } printf ("\ n");    Selection_sort (a); printf ("After the sort:\n");  for(i=0; i<maxsize; i++) {printf ("%d", A[i]); } printf ("\ n");}
View Code

Selection sort of basic algorithm

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.