C Language Implementation bubble Sort method and select Sort method Code Reference

Source: Internet
Author: User

For ease of use, I write sort functions that are similar to those used directly in the keynote function.

  I think the choice of sorting method better understand! Please note that I and J, do not make a mistake when writing code, otherwise it is difficult to find errors!

Bubble Sort Method:

voidSortint* AR,intK//ar points to the array name, K is the number of elements{    inti,j,temp;  for(i =0; I < K-1; i++) {//you can compare k-1.         for(j =0; J < K-i-1; J + +){            if(Ar[j] > ar[j +1]) {temp=Ar[j]; AR[J]= Ar[j +1]; Ar[j+1] =temp; }        }    }} //The second loop explains: After each sequencing, ar[k-i-1] is determined, that is, J Max, Ar[j + 1] is determined. 

Select the sorting method:

1 voidSortint* AR,intK//ar points to the array name, K is the number of elements2 {3     inti,j,temp;4      for(i =0; I < K-1; i++) {//just compare it to the k-1 element.5          for(j = i+1; J < K; J + +){ 6             if(Ar[i] >Ar[j])7             {8temp =Ar[i];9Ar[i] =Ar[j];TenAR[J] =temp; One             } A         } -     } - } the //in the second loop, j = i + 1 is for ar[j] and Ar[i] to compare, i.e. Ar[i + 1] and ar[i], for the first loop, if there is no J < K-1, the array will be out of bounds, which needs to be understood. 

C Language Implementation bubble Sort method and select Sort method Code Reference

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.