Two kinds of implementations of binary lookup method

Source: Internet
Author: User

Two kinds of implementations of binary lookup method

Binary Lookup Method:

In an ordered table, there are three scenarios where the value of the data to be looked up is compared to the median value of the lookup range:

1) to find the data value exactly equal to the intermediate element value, put back the index of the intermediate element value.

2) The data value to be found is smaller than the middle element value, then the first half of the entire look-up range is used as the new look-up range, and 1 is executed until an equal value is located.

3) The data value to be found is larger than the middle element value, then take the second half of the entire look-up range as the new lookup range, execute 1) until an equal value is found

4) If no equal value is found at the end, an error message is returned.

According to the binary tree to understand: The median is two fork tree root, the first half of the left dial hand tree, the second half is the right sub-tree. The binary lookup is exactly the number of layers where the value is located. In the case of equal probability, about

LOG2 (n+1)-1

[CPP]View Plaincopy
  1. Data is the array to find, X is the value to look for, beg is the start of the lookup range, and last is the end of the lookup range
  2. Non-recursive method
  3. int Bisearch (int data[], const int x, int beg, int last)
  4. {
  5. int mid; //Middle position
  6. if (Beg > last)
  7. {
  8. return-1;
  9. }
  10. While (Beg <= last)
  11. {
  12. Mid = (beg + last)/2;
  13. if (x = = Data[mid])
  14. {
  15. return mid;
  16. }
  17. Else if (Data[mid] < x)
  18. {
  19. Beg = mid + 1;
  20. }
  21. Else if (Data[mid] > x)
  22. {
  23. last = mid-1;
  24. }
  25. }
  26. return-1;
  27. }
  28. Recursive method
  29. int Iterbisearch (int data[], const int x, int beg, int last)
  30. {
  31. int mid =-1;
  32. Mid = (beg + last)/2;
  33. if (x = = Data[mid])
  34. {
  35. return mid;
  36. }
  37. Else if (x < Data[mid])
  38. {
  39. return Iterbisearch (data, x, Beg, mid-1);
  40. }
  41. Else if (x > Data[mid])
  42. {
  43. return Iterbisearch (data, X, Mid + 1, last);
  44. }
  45. return-1;
  46. }
  47. Main function
  48. int _tmain (int argc, _tchar* argv[])
  49. {
  50. int data1[60] = {0};
  51. int no2search = 45;
  52. cout << "The array is:" << Endl;
  53. int siz = sizeof (data1)/sizeof (int);
  54. For (int i = 0; i < siz; i++)
  55. {
  56. Data1[i] = i;
  57. cout << Data1[i] << "";
  58. }
  59. cout << Endl;
  60. int index =-1;
  61. //index = Bisearch (data1, No2search, 0, siz);
  62. index = Iterbisearch (data1, No2search, 0, siz);
  63. cout << "Index of" << no2search << "is" << Index << Endl;
  64. GetChar ();
  65. return 0;
  66. }


Two kinds of implementations of binary lookup method

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.