forked from Teju-1212/hacktoberfest-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.java
More file actions
29 lines (23 loc) · 733 Bytes
/
sorting.java
File metadata and controls
29 lines (23 loc) · 733 Bytes
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
public class binarysearch {
// Time complexity: O(N) and auxillaryspace: O(1)
public static void binarysearch(int[] arr, int l, int r, int k) {
int mid = 0;
while (l <= r) {
mid = l + (r - l) / 2;
if (arr[mid] == k) {
System.out.println("element found " + arr[mid]);
break;
}
if (k < arr[mid]) {
r = mid - 1;
} else if(k>arr[mid]){
l = mid + 1;
}
}
System.out.println(arr[mid]);
}
public static void main(String[] args) {
int[] arr = { 5, 9, 17, 23, 25, 45, 59, 63, 71, 89 };
binarysearch(arr, 0, arr.length - 1, 59);
}
}