Implementation of Binary Search Algorithms in Java

Keywords: Java

Binary search:
Two ways: non-recursive and recursive
Main idea: For sorted arrays (assuming that they are sorted from small to large), two "pointers" are defined, one "pointing" to the first element low and one "pointing" to the last element high.
Then, we begin to make a half-way comparison, that is, to compare the number to be searched with the element in the middle of the array (index is low+high/2). To find a smaller number than the middle number,
Explain that the number to be looked up is on the left side of the array (if the array is sorted from small to large), otherwise it is on the right side of the array. If low is larger than high at last,
Two "pointers" crossed, indicating that the number was not found, that is, the number does not exist in the array.

Notes: Sorting rules are related to the sorting order of arrays, that is, sorting from large to small is different from sorting from small to large!!!

 1 class BinarySearch {
 2     
 3     // Binary Search Non-recursive Method
 4     // arr Given a sorted array
 5     // num Number to look for
 6     public static int search(int[] arr, int num) {
 7         int low = 0;
 8         int high = arr.length - 1;
 9         int mid = 0;
10         while (low <= high) {
11             mid = (low + high) / 2;
12             if (num < arr[mid]) {
13                 high = mid - 1;
14             }
15             
16             if (num > arr[mid]) {
17                 low = mid + 1;
18             }
19             if (num == arr[mid]) {
20                 return mid;
21             }
22         }
23         return -1;    // Can't find
24     }
25     
26     
27     // Dichotomous Search Recursive Method
28     // arr Given a sorted array
29     // num Number to look for
30     // low The initial left pointer points to the first element
31     // high The initial end pointer points to the last element
32     public static int binarySearch(int[] arr, int num, int low, int high) {
33         
34         int mid = (low + high) / 2; 
35         
36         // Recursive termination condition
37         if (low > high) {
38             return -1;
39         }
40         
41         if (num < arr[mid]) {
42             return binarySearch(arr, num, low, mid - 1);
43         } else if (num == arr[mid]) {
44             return mid;
45         } else {
46             return binarySearch(arr, num, mid + 1, high);
47         }
48         
49     }
50     
51     
52     public static void main(String[] args) {
53         
54         // Sorting given arrays from small to large.
55         int[] arr = {2, 3, 5, 7, 8, 9, 11, 12, 15};
56         // int[] arr = {15, 12, 11, 9, 8, 7, 5, 3, 2};
57         
58         int m = 3;
59         
60         // int index = search(arr, m);
61         
62         int index = binarySearch(arr, m, 0, arr.length - 1);
63         
64         System.out.println(index);
65         
66     }
67     
68 }

Posted by ttroutmpr on Fri, 04 Oct 2019 00:14:59 -0700