forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchRecursion.java
More file actions
63 lines (57 loc) · 1.97 KB
/
BinarySearchRecursion.java
File metadata and controls
63 lines (57 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.examplehub.searches;
import com.examplehub.maths.MiddleIndexCalculate;
public class BinarySearchRecursion implements Search {
@Override
public int search(int[] numbers, int key) {
return search(numbers, 0, numbers.length - 1, key);
}
/**
* Binary search algorithm using recursion.
*
* @param numbers the numbers to be searched.
* @param left the left index of left sub array.
* @param right the right index of right sub array.
* @param key the key to be searched.
* @return index of {@code key} value if found, otherwise -1.
*/
public int search(int[] numbers, int left, int right, int key) {
if (left > right) {
return -1;
}
int mid = MiddleIndexCalculate.middle(left, right);
if (key == numbers[mid]) {
return mid;
} else if (key > numbers[mid]) {
return search(numbers, mid + 1, right, key); /* search at right sub array */
} else {
return search(numbers, left, mid - 1, key); /* search at left sub array */
}
}
@Override
public <T extends Comparable<T>> int search(T[] array, T key) {
return search(array, 0, array.length - 1, key);
}
/**
* Generic binary search algorithm using recursion.
*
* @param array the array to be searched.
* @param left the left index of left sub array.
* @param right the right index of right sub array.
* @param key the key object to be searched.
* @param <T> the class of the objects in the array.
* @return index of {@code key} if found, otherwise -1.
*/
public <T extends Comparable<T>> int search(T[] array, int left, int right, T key) {
if (left > right) {
return -1;
}
int mid = MiddleIndexCalculate.middle(left, right);
if (key.compareTo(array[mid]) == 0) {
return mid;
} else if (key.compareTo(array[mid]) > 0) {
return search(array, mid + 1, right, key); /* search at right sub array */
} else {
return search(array, left, mid - 1, key); /* search at left sub array */
}
}
}