forked from vishnu2k60/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
31 lines (28 loc) · 796 Bytes
/
BinarySearch.java
File metadata and controls
31 lines (28 loc) · 796 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
30
31
import java.util.Scanner;
class Main{
static int binary_search(int A[], int left, int right, int key)
{
int m;
while( left <= right )
{
m = left + (right-left)/2;
if( A[m] == key ) // Element found
return m;
if( A[m] < key ) // Search in right part of list
left = m + 1;
else // Search in left part of list
right = m - 1;
}
return -1;
}
public static void main(String[] args)
{
int loc, x, array[]={10,11,12,13,14,25,26,37,48,59};
x = 26; // element to be searched in the array
loc=binary_search(array,0,10,x);
if(loc != -1)
System.out.print("Element found at location : " + loc);
else
System.out.print("Element not present in the array.");
}
}