Bubble sort of C-language array + binary lookup method (binary search)

Source: Internet
Author: User

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)

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.