-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Bilinear_Search.dart
97 lines (79 loc) · 1.74 KB
/
Bilinear_Search.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Implementing Bilinear search by using DART.
Given an array of integers, check wether the element to search exists or not in
the array, and if it exists find its position.
*/
import 'dart:io';
int bilinearSearch(var array, int key) {
int front = 0;
int back = array.length - 1;
while (front <= back) {
if (array[front] == key) return front + 1;
if (array[back] == key) return back + 1;
front += 1;
back -= 1;
}
return -1;
}
void main() {
var data = [];
// taking input from user and storing it in data array
var n, ele, key;
print('Enter the number of Elements: ');
n = stdin.readLineSync();
n = int.parse(n);
for (int i = 1; i <= n; i++) {
print('Enter Element $i: ');
ele = stdin.readLineSync();
ele = int.parse(ele);
data.add(ele);
}
print("\nInputted array is $data");
print("\nEntered the element you want to search: ");
key = stdin.readLineSync();
key = int.parse(key);
int found = bilinearSearch(data, key);
if (found != -1)
print("Element found at position: $found");
else
print("Element not found!!!");
}
/*
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
Sample I/O:
Example 1:
Enter the number of Elements:
5
Enter Element 1:
2
Enter Element 2:
23
Enter Element 3:
4
Enter Element 4:
5
Enter Element 5:
6
Inputted array is [2, 23, 4, 5, 6]
Entered the element you want to search:
7
Element not found!!!
Example 2:
Enter the number of Elements:
5
Enter Element 1:
89
Enter Element 2:
34
Enter Element 3:
16
Enter Element 4:
60
Enter Element 5:
33
Inputted array is [89, 34, 16, 60, 33]
Entered the element you want to search:
16
Element found at position: 3
*/