Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions binary_search/binary_search.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
int binarySearch(List<int> arr, int userValue, int min, int max) {
if (max >= min) {

int mid = ((max + min) / 2).floor();
if (userValue == arr[mid]) {
return mid;
} else if (userValue > arr[mid]) {
binarySearch(arr, userValue, mid + 1, max);
} else {
binarySearch(arr, userValue, min, mid - 1);
}
}
return -1;
}

void main() {
List<int> arr = [0, 1, 3, 4, 5, 8, 9, 22];
int userValue = 3;
int min = 0;
int max = arr.length - 1;
int index = binarySearch(arr, userValue, min, max);

if (index != -1) {
print('$x found at positions: $index');
} else {
print('$x Not found');
}
}
20 changes: 20 additions & 0 deletions linear_search/LinearSearch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
int LinearSearch(List<int> a, number) {
for (int i = 0; i < a.length; i++) {
if (a[i] == number) {
return i;
}
}
return -1;
}

void main() {
List<int> list = [10,20,30,40,50,60,70,80,90,100];
int x = 30;
int index = LinearSearch(list, x);

if (index != -1) {
print('$x found at positions: $index');
} else {
print('$x Not found');
}
}