Skip to content

Commit 3df0ecf

Browse files
committed
Add BinarySort in Dart Language
1 parent 320f0eb commit 3df0ecf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int binarySearch<T extends Comparable<Object>>(List<T> sortedList, T value) {
2+
int min = 0;
3+
int max = sortedList.length;
4+
while (min < max) {
5+
final int mid = min + ((max - min) >> 1);
6+
final T element = sortedList[mid];
7+
final int comp = element.compareTo(value);
8+
if (comp == 0) {
9+
return mid;
10+
}
11+
if (comp < 0) {
12+
min = mid + 1;
13+
} else {
14+
max = mid;
15+
}
16+
}
17+
return -1;
18+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Code | Test
3636
------------ | -------------
3737
[Bubble Sort](https://github.com/larissalages/code_problems/blob/master/classical_algorithms/dart/BubbleSort.dart) | Missing tests
3838
[Quick Sort](https://github.com/larissalages/code_problems/blob/master/classical_algorithms/dart/QuickSort.dart) | Missing tests
39+
Bin[Binary Sort](https://github.com/larissalages/code_problems/blob/master/classical_algorithms/dart/BinarySort.dart) | Missing tests
3940

4041
### C++
4142
Code | Test

0 commit comments

Comments
 (0)