C Speech binary search (binary Lookup) algorithm and code

Source: Internet
Author: User

Two-point search also called binary search, its strength is to find fast, the flaw is the request to find the data must be ordered SEQUENCE. The fundamental idea of the algorithm is to look for the sequence of the two positions of the data and the elements to be found to stop comparing, if equal, the performance of the search to win, otherwise it will be based on the status of the series to be searched for two parts of You. Next based on the search sequence of the order of the Order of discipline and the two elements and the size of the search element, to select the element to find the local sequence can exist, to take a strange approach to stop searching, until it is certain that the element to be found can exist, the detailed application of the following code can be detailed understanding.

#include  <stdio.h> binarysearch (int a[], int n, int key) { int  low = 0; int high = n - 1; while (low<= high) {  int mid =  (low + high)/2; int midval = a[mid]; if (midVal< Key)  low = mid + 1; else if (midval>key)  high = mid -  1; else return mid; } return -1; } int main () { int  i, val, ret; int a[8]={-32, 12, 16, 24, 36, 45, 59,  98}; for (i=0; i<8; i++)  printf ("%d\t",  a[i]);  printf ("\ n Please lose the element to be found:");  scanf ("%d", &val);  ret = binarysearch (a,8,val);  if ( -1 == ret)   printf ("find out  \n"); else printf  ("find victory  \n");  return 0; }

Operational consequences:

-32 12 16 24 36 45 59 98 Please output the element you are looking for: 12 find victory

In the following code, we successfully go through the binary search algorithm to complete the search function, as shown in the completion process.

650) this.width=650; "src=" http://c.biancheng.net/cpp/uploads/allimg/140801/1-140P101155Xc.png "style=" border:0px ; width:377px;height:163px; "/>
The search process of binary search algorithm


In the lookup process as shown, the elements of the sequence at both ends are compared with the elements to be searched, and the element bits to be searched are found to be in the left local sequence of the Position. Next to the right of the mid-element as high, continuing to stop the Two-point search, then the corresponding mid-to-side of the element is exactly what to look for the element, find out, go to find the element corresponding to the Subscript. In the main function, go to the value to determine whether the search can win, if the search to win. just print the "search for victory" message, or enter "find out homantin" Information.


This article is from the "11999725" blog, please be sure to keep this source http://12009725.blog.51cto.com/11999725/1843309

C Speech binary search (binary Lookup) algorithm and code

Related Article

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.