-
Notifications
You must be signed in to change notification settings - Fork 390
/
BinarySearch.java
65 lines (53 loc) · 2.04 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
56
57
58
59
60
61
62
63
64
65
import java.util.Scanner;
/*
* Java Program to implement binary search without using recursion
*/
public class BinarySearch {
public static void main(String[] args) {
Scanner commandReader = new Scanner(System.in);
System.out.println("Welcome to Java Program to perform
binary search on int array");
System.out.println("Enter total number of elements : ");
int length = commandReader.nextInt();
int[] input = new int[length];
System.out.printf("Enter %d integers %n", length);
for (int i = 0; i < length; i++) {
input[i] = commandReader.nextInt();
}
System.out.println("Please enter number to be searched in array
(sorted order)");
int key = commandReader.nextInt();
int index = performBinarySearch(input, key);
if (index == -1) {
System.out.printf("Sorry, %d is not found in array %n", key);
} else {
System.out.printf("%d is found in array at index %d %n", key,
index);
}
commandReader.close();
}
/**
* Java method to perform binary search. It accept an integer array and a
* number and return the index of number in the array. If number doesn't
* exists in array then it return -1
*
* @param input
* @param number
* @return index of given number in array or -1 if not found
*/
public static int performBinarySearch(int[] input, int number) {
int low = 0;
int high = input.length - 1;
while (high >= low) {
int middle = (low + high) / 2;
if (input[middle] == number) {
return middle;
} else if (input[middle] < number) {
low = middle + 1;
} else if (input[middle] > number) {
high = middle - 1;
}
}
return -1;
}
}