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