forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.java
43 lines (42 loc) · 1.28 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
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[5];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i< a.length; i++){
a[i] = sc.nextInt();
}
System.out.println("Enter the value to be searched: ");
int x = sc.nextInt();
sc.close();
long start = System.nanoTime();
binary(a, x);
long end = System.nanoTime();
long execution = end - start;
double seconds = (double) execution/ 1000000000.0;
System.out.println("Execution time: " + seconds + " seconds");
}
public static void binary(int a[], int x){
int lo = 0;
int hi = a.length-1;
while (hi - lo>1){
int mid = (hi+lo)/2;
if (a[mid]<x){
lo = mid+1;
}
else {
hi = mid;
}
}
if (a[lo] == x){
System.out.println("Found At Index "+lo);
}
else if (a[hi] == x) {
System.out.println("Found At Index "+hi);
}
else {
System.out.println("Not Found");
}
}
}