Skip to content

Commit 6ea6766

Browse files
authored
Merge pull request larissalages#257 from ALaurinne/2-binary-sort
Add Binary Sort in Dart
2 parents bcd7141 + 2c5ac70 commit 6ea6766

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+
[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)