From 2088e7c115efa72e6103f8fb3c996f3d5e96d667 Mon Sep 17 00:00:00 2001 From: AkhileshManda Date: Sat, 9 Oct 2021 09:13:31 +0530 Subject: [PATCH 1/2] Added Linear Search in Dart --- linear_search/LinearSearch.dart | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 linear_search/LinearSearch.dart 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 From 373464ecfc9091fd8b05fa3826b701c412c4c8b2 Mon Sep 17 00:00:00 2001 From: AkhileshManda Date: Sun, 10 Oct 2021 11:24:02 +0530 Subject: [PATCH 2/2] Added binary search in dart --- binary_search/binary_search.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 binary_search/binary_search.dart 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