Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update binary_Search.dart #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions search/binary_Search.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
int binary_search(List a, int l, int r, int x) {
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 (a[middle] == x) {
if (num[middle] == search) {
return middle;
}

//If the element is smaller than middle
if (a[middle] > x) {
return binary_search(a, l, middle - 1, x);
if (num[middle] > search) {
return binary_search(num, l, middle - 1, search);
}

return binary_search(a, middle + 1, r, x);
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 x = 55;
int search = 55;
int n = list.length;
int index = binary_search(list, 0, n - 1, x);
int index = binary_search(list, 0, n - 1, search);
print('list:');
print(list);
if (index != -1) {
print('$x found at positions: $index');
print('$search found at positions: $index');
} else {
print('$x Not found');
print('$search Not found');
}
Comment on lines 20 to 31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since, We are improiving this file. I would recommend you add some test to this file as well.

}