C-language binary search algorithm and implementation code _c language

Source: Internet
Author: User

two points find also called binary Cha Find, its advantage is to find Fast, the disadvantage is that the need to find the data must be ordered sequence. The basic idea of the algorithm is to compare the data of the intermediate position of the desired sequence with the element to be searched, if it is equal, to find success, otherwise the sequence will be divided into the left and right parts based on the position. Next, according to the order of the ascending and descending sequence and the relationship between the middle element and the found element, to select the part of the sequence you want to find the possible existence of the element, use the same method to search until you can determine the existence of the element you are looking for, and the specific method can be understood in the following code.

#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, k, N, K, K, K;
 For (i=0 i<8; i++)
  printf ("%d\t", A[i]);
 printf ("\ nthe element to be searched for:");
 scanf ("%d", &val);
 ret = BinarySearch (a,8,val);
 if ( -1 = ret)
  printf ("Find failed \ n");
 else
  printf ("Find success \ n");
 return 0;
}

Run Result:

-32 12 16 24 36 45 59 98

Please enter the element you are looking for: 12

Find success

In the above code, we successfully through the binary search algorithm to achieve the lookup function, the implementation process as shown in the following figure.

In the search process shown above, the elements in the middle of the sequence are compared to the elements to be searched, and the elements to be searched are found in the left sequence of the position. Next, mid to the left of an element as high, continue to the two-point search, then mid of the middle element is just the element to find, Cha find the end, return to find the corresponding elements of the subscript. In the main function, the return value is used to determine if the search is successful or unsuccessful. Print out "Find success" information, otherwise output "Cha find lost 畋" information.

The above is the two-point search method of detailed introduction, hope to learn C language students can master.

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.