forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinearSearch.dart
56 lines (51 loc) · 1.23 KB
/
LinearSearch.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
A linear search or sequential search is a method for finding an element within a list.
It sequentially checks each element of the list until a match is found or the whole list has been searched
*/
import 'dart:io';
linearSearch(List array, int toFind) {
for (int index = 0; index < array.length; index++) {
if (array[index] == toFind) {
// If element is found return index at which it is found
return index;
}
}
// If element is not found return -1
return -1;
}
void main() {
//Driver code
print("Enter List size: ");
int listSize = int.parse(stdin.readLineSync()!);
var listUser = [];
// Take list input
print("Enter $listSize elements: ");
for (int i = 0; i < listSize; i++) {
int userInput = int.parse(stdin.readLineSync()!);
listUser.add(userInput);
}
print("Enter Element to search: ");
int toFind = int.parse(stdin.readLineSync()!);
// Search and display result
int result = linearSearch(listUser, toFind);
if (result == -1) {
print("$toFind not found in list");
} else {
print("$toFind found at index $result");
}
}
/**
Time complexity: O(n)
Space complexity: O(1)
Enter List size:
5
Enter 5 elements:
1
2
3
4
5
Enter Element to search:
4
4 found at index 3
*/