Skip to content

Commit

Permalink
Merge pull request shivaylamba#570 from jayprajapati47/master
Browse files Browse the repository at this point in the history
Create Added Binary-Search.dart
  • Loading branch information
shivaylamba authored Oct 3, 2020
2 parents ea4662d + 58d3932 commit 0b6beb2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Binary-Search.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
void main() {
List<int> arr = [0, 1, 3, 4, 5, 8, 9, 22];
int userValue = 3;
int min = 0;
int max = arr.length - 1;
binarySearch(arr, userValue, min, max);
}

binarySearch(List<int> arr, int userValue, int min, int max) {
if (max >= min) {
print('min $min');
print('max $max');
int mid = ((max + min) / 2).floor();
if (userValue == arr[mid]) {
print('your item is at index: ${mid}');
} else if (userValue > arr[mid]) {
binarySearch(arr, userValue, mid + 1, max);
} else {
binarySearch(arr, userValue, min, mid - 1);
}
}
return null;
}

/*
Output
min 0
max 7
min 0
max 2
min 2
max 2
your item is at index: 2
*/

0 comments on commit 0b6beb2

Please sign in to comment.