diff --git a/binary_search/binary_search.dart b/binary_search/binary_search.dart new file mode 100644 index 00000000..7d898807 --- /dev/null +++ b/binary_search/binary_search.dart @@ -0,0 +1,28 @@ +int binarySearch(List 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 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'); + } +} \ No newline at end of file diff --git a/linear_search/LinearSearch.dart b/linear_search/LinearSearch.dart new file mode 100644 index 00000000..813a2848 --- /dev/null +++ b/linear_search/LinearSearch.dart @@ -0,0 +1,20 @@ +int LinearSearch(List a, number) { + for (int i = 0; i < a.length; i++) { + if (a[i] == number) { + return i; + } + } + return -1; +} + +void main() { + List 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'); + } +} \ No newline at end of file