-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
48 lines (41 loc) · 1.27 KB
/
BinarySearch.java
File metadata and controls
48 lines (41 loc) · 1.27 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
public class BinarySearch {
public static int BinarySearch(int numbers[], int key) { // Time Complexity =O(log n)
int start =0;
int end = numbers.length - 1;
while (start <= end) {
int mid =(start + end ) / 2;
//comprision
if (numbers[mid] == key) {
return mid;
}
if (numbers[mid] < key) { // right
start =mid+1;
}else{ // left
end = mid - 1;
}
}
return -1;
}
public static void reverse(int numbers[]) {
int first = 0; int last = numbers.length-1;
while (first<last) {
//swaps
int temp = numbers[last];
numbers[last] = numbers[first];
numbers[first] = temp;
first++;
last--;
}
}
public static void main(String[] args) {
int numbers[] = {2,4,6,8,10,12,14,16,18,20};
// int key = 10;
// System.out.println("Index of number is : " + BinarySearch(numbers , key));
reverse(numbers);
//print
for(int i =0;i<numbers.length;i++){
System.out.print(numbers[i] + " ");
}
System.out.println();
}
}