-
Notifications
You must be signed in to change notification settings - Fork 97
/
binarySearch.java
55 lines (46 loc) · 1.4 KB
/
binarySearch.java
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
49
50
51
52
53
54
55
import java.util.Scanner;
class BinarySearch{
/**
* this function contains the code for iterative binary search
* @param array
* @param find
* @return int
**/
public static int binarySearch(int array[], int find){
int startIndex = 0, endIndex = array.length, midIndex;
while( startIndex < endIndex ){
midIndex = (startIndex + endIndex) / 2;
if( array[midIndex] == find ){
return midIndex;
}
else if( find < array[midIndex] ){
endIndex = midIndex;
}
else{
startIndex = midIndex+1;
}
}
return -1;
}
public static void main(String args[]){
int size, find, loc;
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of array = ");
size = in.nextInt();
int array[] = new int[size];
System.out.println("Enter " + size + " sorted elements in array :");
for(int i=0; i<size; i++){
array[i] = in.nextInt();
}
System.out.print("Enter value to find = ");
find = in.nextInt();
loc = binarySearch(array, find);
if( loc == -1 ){
System.out.println("value Not found in array");
}
else{
System.out.println("value found at index " + loc);
}
in.close();
}
}