-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
52 lines (46 loc) · 1.82 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
package binary.search;
import java.util.Scanner;
/**
* Based on the sorted array
* Divide the array by means of middle index
* The if the search value is greater than the middle index take only the right half array
* Else if the search element id less than the middle element then take only the left element
* Else if the middle element is equal to the search element then value is found
*/
public class BinarySearch {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Array size:");
int size=scan.nextInt();
int start=0;
int end=size;
int[] array=new int[size];
for(int i=0;i<size;i++){
System.out.println("Element in array:");
int element=scan.nextInt();
array[i]=element;
}
System.out.println("Enter the value to be searhed:");
int value=scan.nextInt();
binarySearch(array,start,end,value);
}
public static void binarySearch(int[] array,int start,int end,int svalue){
int middle=0;
middle=(start+end)/2;
if(start<=end){
if(array[middle]>svalue){
end=middle-1;
middle=(start+end)/2;
System.out.println("Value found at the position:"+middle+" and the value is :"+array[middle]);
}
else if(array[middle]<svalue){
start=middle+1;
middle=(start+end)/2;
System.out.println("Value found at the position:"+middle+" and the value is :"+array[middle]);
}
else if(array[middle]==svalue){
System.out.println("Value found at the position:"+middle+" and the value is :"+array[middle]);
}
}
}
}