Several common sorting algorithms in C language

Source: Internet
Author: User

1, Bubble sorting method: If there are n number, 22 adjacent to compare, will sink the most value into the last position, to do N-1 wheel comparison,

The first round to compare N-1 times, the 2nd round as long as the comparison n-1-1 times, because the first time has already sunk the most value into the last position, so

Does not need to be compared once, that is, each round is reduced one time, so the 2nd for loop J < n-1; The following code is sorted in ascending order

1 voidBubble_sort (int*num,intN)2 {3     intnum_temp;4      for(intI=0; i<n; i++)  5          for(intj=0; j<n-i;j++)6         {7             if(Num[j] > num[j+1])8             {9Num_temp =Num[j];TenNUM[J] = num[j+1]; Onenum[j+1] =num_temp; A             } -         } -}

2. Select sort: Each cycle (that is, the 2nd for loop) finds only the position of the minimum value, but does not exchange the data.

After the end of the round, judging whether the minimum mark is the same as the original hypothesis, if not the same, the other position is smaller (or larger),

When the data is swapped, the data is exchanged only once per round cycle. And in the bubble sort, the 2nd loop, just find the bad

The value will be exchanged, and multiple exchanges will be performed in a round loop. Bubble Sorting is therefore more time-consuming than selecting a sort. The following code is sorted in descending order

1 voidSelect_sort (int*num,intN)2 {3     intk_min;4     intNum_temp =0;5 6      for(intI=0; i<n-1; i++)  7     {8K_min = i;//assuming the first minimum9          for(intj=i+1; j<n; J + +)Ten         { One             if(Num[k_min] <Num[j]) A             { -K_min =J; -             } the         } -  -         if(K_min! = i)//data Exchange -         { +Num_temp =Num[i]; -Num[i] =Num[k_min]; +Num[k_min] =num_temp; A         } at     } -}

Overall code:

1#include <iostream.h>2 #defineN 103 4 voidBubble_sort (int*num,intN)5 {6     intnum_temp;7      for(intI=0; i<n; i++)  8          for(intj=0; j<n-i;j++)9         {Ten             if(Num[j] > num[j+1]) One             { ANum_temp =Num[j]; -NUM[J] = num[j+1]; -num[j+1] =num_temp; the             } -         } - } -  + voidSelect_sort (int*num,intN) - { +     intk_min; A     intNum_temp =0; at  -      for(intI=0; i<n-1; i++)   -     { -K_min = i;//assuming the first minimum -          for(intj=i+1; j<n; J + +) -         { in             if(Num[k_min] <Num[j]) -             { toK_min =J; +             } -         } the  *         if(K_min! = i)//data Exchange $         {Panax NotoginsengNum_temp =Num[i]; -Num[i] =Num[k_min]; theNum[k_min] =num_temp; +         } A     } the } +  - intMain () $ { $     intNum[n] = { the,6, A,9, the,8, -, -,1,3}; -  - //Bubble_sort (num,n); the Select_sort (num,n); - Wuyi      for(intI=0; i<n; i++) the     { -cout<<num[i]<<Endl; Wu     } -  About     return 0; $}
View Code

Several common sorting algorithms in C language

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.