Bubble Sort algorithm
1 intnum[5];2     inti;3     //iterating over the elements entered by the user4      for(i=0;i<5; i++){5printf"Please enter%d elements \ n", i+1);6scanf"%d",&num[i]);7     }8      for(i=0;i<4; i++) {//Outer Loop, controlling the number of rounds to sort9         intJ;Ten          for(j=0;j<4-i;j++) {//Inner Loop to control the number of counterfeit orders per round One             if(num[j]>num[j+1]){//determines the size of the judging element and the latter element, and if the preceding element is large, the swap position A                 inttemp; -temp=Num[j]; -num[j]=num[j+1]; thenum[j+1]=temp; -             } -         } -     } +     //traversing a printed array -      for(i=0;i<5; i++){ +printf"%d\t", Num[i]); A}
Binary Lookup method (binary lookup, if it must be an ordered array)
1 intnum[Ten]={1,2,3,4,5,6,7,8,9,Ten};2     intSearch;3printf"Please enter the element you want to find \ n");4scanf"%d",&search);5     intMiddle;//Middle Position6     intleft=0;//start subscript, default first element subscript7     intright=9;//end subscript, default subscript for last element8     intCount=0;//number of records queried9     intflag=0;//Mark to determine if there is an element;Ten      while(left<=Right ) { Onecount++; AMiddle= (left+right)/2; -         if(Search>num[middle]) {//the element to be found must have a large value in the middle position element, look to the right, change the starting position -Left=middle+1; the}Else if(Search<num[middle]) {//the element to be found must have a small value in the middle position element, find it on the left, change the end position -right=middle-1; -}Else{ -printf"The feature%d you are looking for is labeled%d", search,middle); +flag=1; -              Break; +         } A     } at     if(flag==1){ -printf"you've used%d times to find this element \ n", count); -}Else{ -printf"the element you are looking for does not exist \ n"); -}
Bubble sort of C-language array + binary lookup method (binary search)