Skip to content

Commit 4a3107c

Browse files
Create Searching and Sorting:Binary search
1 parent 8d2ecf6 commit 4a3107c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Searching and Sorting:Binary search

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class BinarySearch{
2+
public static int binarySearch(int[] arr, int num){
3+
int start=0;
4+
int end=arr.length-1;
5+
while(start<=end)
6+
{
7+
int mid=(start+end)/2;
8+
if(num==arr[mid])
9+
{
10+
return mid;
11+
}
12+
else if(num>arr[mid])
13+
{
14+
start=mid+1;
15+
16+
}
17+
else
18+
{
19+
20+
end=mid-1;
21+
22+
}
23+
}
24+
return -1;
25+
}
26+
}

0 commit comments

Comments
 (0)