Skip to content

Commit 9bf4bfb

Browse files
authored
BinarySearch
1 parent bde484b commit 9bf4bfb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

binary.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class BinarySearch{
2+
public static void binarySearch(int arr[], int first, int last, int key){
3+
int mid = (first + last)/2;
4+
while( first <= last ){
5+
if ( arr[mid] < key ){
6+
first = mid + 1;
7+
}else if ( arr[mid] == key ){
8+
System.out.println("Element is found at index: " + mid);
9+
break;
10+
}else{
11+
last = mid - 1;
12+
}
13+
mid = (first + last)/2;
14+
}
15+
if ( first > last ){
16+
System.out.println("Element is not found!");
17+
}
18+
}
19+
public static void main(String args[]){
20+
int arr[] = {10,20,30,40,50};
21+
int key = 30;
22+
int last=arr.length-1;
23+
binarySearch(arr,0,last,key);
24+
}
25+
}

0 commit comments

Comments
 (0)