Java binary search

Description: Welcome to criticize and correct, leave a message and like! If reprinting, please indicate the original address: http://www.cnblogs.com/chris0710/p/8995353.html

This article will illustrate the principle of binary search through a simple example, gossip and go directly to the code.

1  /** 
2  * Binary Search
 3  *
 4   */ 
5  public  class BinarySearchDemo {
 6      public  static  void main(String[] args) {
 7          int [] arr = {1, 2, 3, 4, 5, 7 };
 8          int len ​​= arr.length;
 9          // call binary search method 
10          int index1 = whileBinarySearch(arr, 4 );
 11          int index2 = recursionBinarySearch(arr, 4, 0, len-1 );
 12          int index3 = whileBinarySearch(arr , 6 );
 13          intindex4 = recursionBinarySearch(arr, 6, 0, len-1 );
 14          System.out.println("Use the binary search of the while loop to find the position of 4: " + index1);
 15          System.out.println("Use the recursive binary search Find the position of 4: " + index2);
 16          System.out.println("Use the binary while loop to find the position of 6: " + index3);
 17          System.out.println("Use recursive binary to find the position of 6: " + index4);
 18          /** 
19           Console output result:
 20               Using the while loop binary to find the position of 4: 3
 21               Using recursive binary to find the position of 4: 3
 22               Using the while loop to find the position of 6:- 1
 23               Use recursive binary to find the position of 6: -1
 24           */ 
25      }
26 
27     /**
28      * 使用while循环进行二分查找
29      */
30     public static int whileBinarySearch(int[] arr, int a) {
31         int low = 0;
32         int high = arr.length -1;
33         if (arr[low] > a || arr[high] < a || low > high) {
34             return -1;
35         }
36         while (low <= high) {
37             int middle = (low + high)/2;
38             if(arr[middle] > a) { // a is on the left half of the array 
39                  high = middle - 1 ;
 40              } else  if (arr[middle] < a) { // a is on the right half of the array 
41                  low = middle + 1 ;
 42              } else {
 43                  return middle;
 44              }
 45          }
 46          return -1 ;
 47      }
 48      /** 
49       * binary search using recursion
 50       */ 
51      public  static  int recursionBinarySearch(int[] arr, int a, int low, int high) {
52         int middle = (low + high) / 2;
53         if (arr[low] > a || arr[high] < a || low > high) {
54             return -1;
55         }
56         if (arr[middle] > a) {
57             return recursionBinarySearch(arr, a, low, middle-1);
58         } else if (arr[middle] < a) {
59             return recursionBinarySearch(arr, a, middle+1, high);
60         } else {
61             return middle;
62         }
63     }
64 }

Finally: recommend a super practical visualization tool, the purpose is to interactively see the status and parameters of each step of the program, it is perfect!

Visual tool address: http://www.pythontutor.com/java.html#mode=edit

The interface effect diagram is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305925&siteId=291194637
Recommended