C language--several kinds of classical sorting algorithms

Source: Internet
Author: User

Classic sort of bubble sort

#defineSIZE 5intMain () {intA[size] = {0}; //Loop Input     for(intI=0; i<size;i++) {scanf ("%d",&A[i]); }        //Bubble Sort Core     for(intI=0; i<size-1; i++)    {         for(intj=0; j<size-1-i;j++)        {            if(a[j]>a[j+1])            {                inttemp =A[j]; A[J]= a[j+1]; A[j+1] =temp; }        }    }        //Traverse Output     for(intI=0; i<size;i++) {printf ("%d", A[i]); }}

Select Sort:

Classic Sorting algorithm - Select sort Selection Sort

finds the smallest number in an array, swapping with the first element

1 #defineSIZE 52 intMain ()3 {4     intA[size] = {0};5     //Loop Input6      for(intI=0; i<size;i++)7     {8scanf"%d",&a[i]);9     }Ten      One      for(intI=0; i<size-1; i++) A     { -         intK =i; -          for(intj=i+1; j<size;j++) the         { -             if(a[k]>A[j]) -             { -K =J; +             } -         } +         if(k! =i) A         { at             inttemp =A[k]; -A[K] =A[i]; -A[i] =temp; -         } -     } -      in     //Traverse Output -      for(intI=0; i<size;i++) to     { +printf"%d", A[i]); -     } the}

C language--several kinds of classical sorting algorithms

Related Article

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.