-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.java
More file actions
57 lines (44 loc) · 1.78 KB
/
Recursion.java
File metadata and controls
57 lines (44 loc) · 1.78 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
package recursion;
public class Recursion {
public static int find(int[] arr, int target) {
return find(arr, target, 0);
}
public static int find(int[] arr, int target, int i) {
if (i == arr.length) return -1;
if (arr[i] == target) return i;
return find(arr, target, i + 1);
}
public static int binary_search(int[] arr, int target) {
return binary_search(arr, target, 0, arr.length);
}
public static int binary_search(int[] arr, int target, int i, int j) {
if (i >= j) return -1;
int mid = (i + j) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) i = mid + 1;
else j = mid - 1;
return binary_search(arr, target, i, j);
}
public static int bisect_right(int[] arr, int target) {
return bisect_right(arr, target, 0, arr.length);
}
public static int bisect_right(int[] arr, int target, int i, int j) {
if (i == j) return i;
int mid = (i + j) / 2;
if (arr[mid] <= target) i = mid + 1;
else j = mid;
return bisect_right(arr, target, i, j);
}
public static void main(String[] args) {
System.out.println(find(new int[]{1, 2, 3}, 3));
System.out.println(find(new int[]{1, 2, 3}, 4));
System.out.println(binary_search(new int[]{1, 2, 2, 3}, 4));
System.out.println(binary_search(new int[]{1, 2, 2, 3}, 2));
System.out.println(binary_search(new int[]{1, 2, 2, 3}, 0));
System.out.println(bisect_right(new int[]{1, 2, 2, 3}, 4));
System.out.println(bisect_right(new int[]{1, 2, 2, 3}, 2));
System.out.println(bisect_right(new int[]{1, 2, 2, 3}, 0));
Recursion recursion = new Recursion();
System.out.println(recursion.toString());
}
}