Binary lookup algorithm (recursive vs. non-recursive two ways)

Source: Internet
Author: User

First of all, the binary search method.

Binary search method is to find a set of ordered numbers, pass the corresponding data, compare to find the same data as the original data, find the return 1, failed to return the corresponding array subscript.

The two-point search method was completed by non-recursive method. The Java code is shown below.

[Java]View PlainCopyprint?
  1. /* 
  2. * Non-recursive binary search algorithm
  3. * Parameter: integer array, the number to compare.
  4. */
  5. public static int BinarySearch (integer[]srcarray,int des) {
  6. //First position.
  7. int low=0;
  8. //Highest position. Array Length-1, since the subscript is starting from 0.
  9. int high=srcarray.length-1;
  10. //When Low "pointer" and high do not repeat.
  11. While (Low<=high) {
  12. //Middle position calculation, low+ the highest position minus the lowest position, move right one bit, equivalent to 2. can also be used (High+low)/2
  13. int middle=low+ ((high-low) >>1);
  14. and//with the most intermediate numbers to determine whether they are equal or equal, the corresponding array subscripts are returned.
  15. if (Des==srcarray[middle]) {
  16. return middle;
  17. //If less then move the "pointer" at the top level
  18. }Else if (Des<srcarray[middle]) {
  19. high=middle-1;
  20. //Move the lowest "pointer"
  21. }else{
  22. Low=middle+1;
  23. }
  24. }
  25. return-1;
  26. }
  27. }



Recursive method is used to complete the two-point search algorithm. The code is shown below.

[Java]View PlainCopy print?
  1. /**
  2. * Recursive method to realize the binary search method.
  3. * @param array arrays
  4. * @param low Array First position
  5. * @param high
  6. * @param key to find the value.
  7. * @return return value.
  8. */
  9. int Binsearch (int array[],int low,int high,int key)
  10. {
  11. if (Low<=high)
  12. {
  13. int mid = (Low+high)/2;
  14. if (key = = Array[mid])
  15. return mid;
  16. Else if (Key<array[mid])
  17. //Move low and high
  18. return Binsearch (array,low,mid-1,key);
  19. Else if (Key>array[mid])
  20. return Binsearch (array,mid+1,high,key);
  21. }
  22. Else
  23. return-1;
  24. }


Recursive thinking is often used, more prominent programming problem-solving efficiency.

Excerpt from: http://blog.csdn.net/lovesummerforever/article/details/24588989

Binary lookup algorithm (recursive vs. non-recursive two ways)

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.