-
Notifications
You must be signed in to change notification settings - Fork 61
/
BinarySearch.java
61 lines (57 loc) · 1.57 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
package com.algorithm.search;
/*
* Binary Search
* --------------
* In computer science, binary search, also known as
* half-interval search is a search algorithm that
* finds the position of a target value within a sorted array.
*
* Binary search compares the target value to the middle element
* of the array. If they are not equal, the half in which
* the target cannot lie is eliminated and the search continues
* on the remaining half, again taking the middle element
* to compare to the target value, and repeating this until
* the target value is found. If the search ends with the
* remaining half being empty, the target is not in the array.
*
* Time Complexity
* ----------------
* Worst Case O(log n)
* Best Case O(1)
*
* This search is faster than linear search.
*/
public class BinarySearch {
public static void main(String[] args) {
int array[] = { 10, 20, 30, 40, 50 };
int key = 50;
int index = binarySearch(array, key);
if(index == -1)
System.out.println("Element NOT found.");
else
System.out.println("Element found at index : "+index);
}
public static int binarySearch(int array[], int key) {
int leftIndex = 0, rightIndex = array.length - 1;
while (leftIndex <= rightIndex) {
int mid = (leftIndex + rightIndex) / 2;
if (array[mid] == key)
return mid;
if (array[mid] < key)
leftIndex = mid + 1;
else
rightIndex = mid - 1;
}
return -1;
}
}
/*
OUTPUT
array[] = { 10, 20, 30, 40, 50 }
key = 50
Element found at index : 4
OUTPUT
array[] = { 10, 20, 30, 40, 50 }
key = 100
Element NOT found.
*/