-
Notifications
You must be signed in to change notification settings - Fork 9
/
binary_search.dart
55 lines (50 loc) · 1.72 KB
/
binary_search.dart
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
/// Write a function called binarySearch which accepts a sorted array and a value
/// and returns the index at which the value exists. Otherwise, return -1.
///
/// binarySearch([1,2,3,4,5],2) // 1
/// binarySearch([1,2,3,4,5],3) // 2
/// binarySearch([1,2,3,4,5],5) // 4
/// binarySearch([1,2,3,4,5],6) // -1
/// binarySearch([
/// 5, 6, 10, 13, 14, 18, 30, 34, 35, 37,
/// 40, 44, 64, 79, 84, 86, 95, 96, 98, 99
/// ], 10) // 2
/// binarySearch([
/// 5, 6, 10, 13, 14, 18, 30, 34, 35, 37,
// 40, 44, 64, 79, 84, 86, 95, 96, 98, 99
/// ], 95) // 16
/// binarySearch([
/// 5, 6, 10, 13, 14, 18, 30, 34, 35, 37,
/// 40, 44, 64, 79, 84, 86, 95, 96, 98, 99
/// ], 100) // -1
///
/// Time Complexity: Best O(1) - Worst O(log n) - Average O(log n)
/// Auxiliary Space: O(1)
void main() {
print(binarySearch([1, 2, 3, 4, 5], 5));
}
// Input: a sorted array and a value
// Output: the index at which the value exists, or -1 if not found
int binarySearch(List<int> arr, int val) {
// Declare the left,right variables to first and last items
int left = 0;
int right = arr.length - 1;
// While left is <= right:
// Get the middle point between left and right.
// If the value is greater than the middle item set left to the middle + 1.
// If the value is less than the middle item set right to the middle - 1.
// or else, that means the middle item is the wanted value, then return its index.
while (left <= right) {
final int middle = ((right + left) / 2).floor();
final int middleItem = arr[middle];
if (val > middleItem) {
left = middle + 1;
} else if (val < middleItem) {
right = middle - 1;
} else {
return middle;
}
}
// Eventually, return -1 if the value is not found
return -1;
}