Algorithm of _001_ binary search algorithm

Source: Internet
Author: User

Binary search algorithm is one of the more frequent algorithms used in ordered arrays, in the non-contact binary lookup algorithm, the most common approach is to traverse the array, compare each element, and its time is O (n). But the binary lookup algorithm is better because it has a lookup time of O (LGN), such as the array {1, 2, 3, 4, 5, 6, 7, 8, 9}, find element 6, executed with binary lookup algorithm, in the order of:

1. The first step is to find the intermediate element, that is, 5, because 5<6, 6 is necessarily in the array element after 5, then it is found in {6, 7, 8, 9},
2. Looking for the median of {6, 7, 8, 9}, which is 7,7>6, 6 should be in the array element to the left of 7, then only 6 is found.

The binary search algorithm is to continuously divide the array into half, and compare the intermediate elements with goal each time.

Realization of--java code with binary search method

1, premise: The premise of binary search is to find the array must be sorted, our implementation here is the default ascending

2, principle: The array is divided into three parts, in order is the median (the so-called median is the middle of the array value) before, median, median value, the value to be looked up and the median of the array to compare, if less than the median value is in front of the median value is found, if the value is greater than the median value after the Then there is a recursive procedure, in which the first half or the second part continues to be decomposed into three parts. may not be described very clearly, if you do not understand can go online to find. It can be seen from the description that this algorithm is suitable for recursive implementation, can be used recursively can be used to achieve the loop. So our implementation is divided into recursive and cyclic two, can be based on the code to understand the algorithm

3, implementation: code as follows

[Java]View PlainCopy
  1. Package org.cyxl.algorithm.search;
  2. /**
  3. * Two-point search
  4. * @author CYXL
  5. *
  6. */
  7. Public class BinarySearch {
  8. private int rcount=0;
  9. private int lcount=0;
  10. /** 
  11. * Number of times to get recursion
  12. * @return
  13. */
  14. public int Getrcount () {
  15. return rcount;
  16. }
  17. /** 
  18. * Number of times to get loops
  19. * @return
  20. */
  21. public int Getlcount () {
  22. return lCount;
  23. }
  24. /** 
  25. * Performs a recursive binary lookup, returning the position where the value first appears
  26. * @param sorteddata sorted array
  27. * @param start position
  28. * @param End Position
  29. * @param findvalue need to find the value
  30. * @return The position of the value in the array, starting at 0. No return-1 found
  31. */
  32. public int searchrecursive (int[] sorteddata,int start,int end,int findvalue) /c4>
  33. {
  34. rcount++;
  35. if (start<=end)
  36. {
  37. //Middle position
  38. int middle= (start+end) >>1; //equivalent (Start+end)/2
  39. //Middle value
  40. int Middlevalue=sorteddata[middle];
  41. if (findvalue==middlevalue)
  42. {
  43. //equals median return directly
  44. return middle;
  45. }
  46. Else if (findvalue<middlevalue)
  47. {
  48. //Less than mid value when looking in front of the median value
  49. return searchrecursive (sorteddata,start,middle-1,findvalue);
  50. }
  51. Else
  52. {
  53. //greater than median is found after median value
  54. return searchrecursive (sorteddata,middle+1,end,findvalue);
  55. }
  56. }
  57. Else
  58. {
  59. //Not found
  60. return-1;
  61. }
  62. }
  63. /** 
  64. * Loop binary lookup, returns the position of the first occurrence of the value
  65. * @param sorteddata sorted array
  66. * @param findvalue need to find the value
  67. * @return The position of the value in the array, starting at 0. No return-1 found
  68. */
  69. public int Searchloop (int[] sorteddata,int findvalue)
  70. {
  71. int start=0;
  72. int end=sorteddata.length-1;
  73. While (start<=end)
  74. {
  75. lcount++;
  76. //Middle position
  77. int middle= (start+end) >>1; //equivalent (Start+end)/2
  78. //Middle value
  79. int Middlevalue=sorteddata[middle];
  80. if (findvalue==middlevalue)
  81. {
  82. //equals median return directly
  83. return middle;
  84. }
  85. Else if (findvalue<middlevalue)
  86. {
  87. //Less than mid value when looking in front of the median value
  88. end=middle-1;
  89. }
  90. Else
  91. {
  92. //greater than median is found after median value
  93. Start=middle+1;
  94. }
  95. }
  96. //Not found
  97. return-1;
  98. }
  99. }


4. Test code

[Java]View PlainCopy
  1. Package org.cyxl.algorithm.search.test;
  2. Import Org.cyxl.algorithm.search.BinarySearch;
  3. Import Org.junit.Test;
  4. Public class Binarysearchtest {
  5. @Test
  6. public void Testsearch ()
  7. {
  8. BinarySearch bs=New BinarySearch ();
  9. int[] sorteddata={1,2,3,4,5,6,6,7,8,8,9,10};
  10. int findvalue=9;
  11. int length=sorteddata.length;
  12. int pos=bs.searchrecursive (Sorteddata, 0, length-1, findvalue);
  13. System.out.println ("Recursice:" +findvalue+"found in pos" +pos+"; Count:" +bs.getrcount ());
  14. int Pos2=bs.searchloop (sorteddata, findvalue);
  15. System.out.println ("Loop:" +findvalue+"found in pos" +pos+"; Count:" +bs.getlcount ());
  16. }
  17. }


5, Summary: This way of finding the use of the situation is a sorted array. You can see that recursion and looping are the same number of times.

Algorithm of _001_ binary search algorithm

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.