-
-
Notifications
You must be signed in to change notification settings - Fork 488
Expand file tree
/
Copy pathbinary_Search.dart
More file actions
32 lines (28 loc) · 751 Bytes
/
binary_Search.dart
File metadata and controls
32 lines (28 loc) · 751 Bytes
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
int binary_search(List num, int l, int r, int search) {
if (r >= l) {
int middle = (l + (r - l) / 2).toInt();
//If the element is present at middle
if (num[middle] == search) {
return middle;
}
//If the element is smaller than middle
if (num[middle] > search) {
return binary_search(num, l, middle - 1, search);
}
return binary_search(num, middle + 1, r, search);
}
return -1;
}
void main() {
List list = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
int search = 55;
int n = list.length;
int index = binary_search(list, 0, n - 1, search);
print('list:');
print(list);
if (index != -1) {
print('$search found at positions: $index');
} else {
print('$search Not found');
}
}