forked from yshshrm/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbinary_search.java
More file actions
28 lines (25 loc) · 745 Bytes
/
Copy pathbinary_search.java
File metadata and controls
28 lines (25 loc) · 745 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
public class BinarySearch {
public boolean search(int arr[], int key){
int low = 0;
int high = arr.length - 1;
int mid, swap;
for(int i=arr.length-1; i>0; i--)
for(int j=0; j<i; j++){
if(arr[j] > arr[j+1]){
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
while(low <= high){
mid = (low + high) / 2;
if(arr[mid] == key)
return true;
else if(key > arr[mid])
low = mid + 1;
else if(key < arr[mid])
high = mid - 1;
}
return false;
}
}