C Language Search Algorithm order lookup, binary lookup (binary lookup)

Source: Internet
Author: User

C Language Search Algorithm order lookup, binary search (binary find), the recent test to use, the Internet also has a lot of examples, I think I wrote to understand some.

  1. Sequential Lookup
    /*Sequential Lookup order lookups are the exact locations of the same number of given keywords in a known no (or ordered) Order queue. The principle is to have the keyword match the number in the queue from the last one (or the first one), until it finds the same number as the given keyword
    Its disadvantage is inefficiency.*/#include<stdio.h>voidMain () {intI, num,arr[Ten]={3,6,9,Ten, $, at, the, $,2, One}; intSize =sizeof(arr)/sizeof(int); printf ("Please enter a value to query:"); scanf ("%d",&num); for(i=0; i<size;i++){ if(num==Arr[i]) { Break; } } if(i!=size) printf ("the value to query%d in the first%d locations", num,i+1); Elseprintf ("value%d not found", num); Getch ();}

  2. Two-point Search
    /*Binary Lookup Two-point lookup is also called binary lookup (binary search), which is a more efficient method of finding. However, the binary lookup requires that the linear table must take a sequential storage structure, and that the elements in the table are ordered alphabetically by keyword. */#include<stdio.h>voidMain () {intmid,low,high,num,arr[Ten]={3,6,9,Ten, at, $, the, the, the,213}; intSize =sizeof(arr)/sizeof(int); printf ("Please enter a value to query:"); scanf ("%d",&num); Low=0; High= size;//values for initial low and high     while(low<=High ) {Mid= (Low+high)/2;//Take medium value        if(Arr[mid]==num) Break;//Find, end loop at this time Low<=high        Else if(Arr[mid] < num) Low = mid+1;//if the target value is larger than the current middle value, the target value is moved to mid+1 after the median value        ElseHigh = mid-1; }    if(Low <= High)//Find, Outputprintf"the value to query%d in the first%d locations", num,mid+1); Else //not foundprintf"value%d not found", num); Getch ();} 

C Language Search Algorithm order lookup, binary lookup (binary lookup)

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.