forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search.dart
149 lines (143 loc) · 3.81 KB
/
Binary_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//Binary Search is an efficient algorithm for finding an item from a sorted list of items.
//This is the implementation of Binary Search in dart language to insert and search the elements.
import 'dart:io';
void binarySearch(List<int> list, int x){
int size = list.length;
int low = 0;
int high = size - 1;
while(low <= high){
int mid = ((low + high) / 2).floor();
if(list[mid] == x){
print('Found $x in the List at Index $mid!');
return;
}
if(list[mid] > x){
high = mid - 1;
continue;
}
low = mid + 1;
}
print('Did not $x in the List!');
return;
}
main(){
List<int> list = new List();
int task = 0;
print('Binary Search Program');
while(task != 4){
print('0 - Display List\n1 - Insert element in List \n2 - Search element in List\n3 - Quit\nSelect task:-');
try{
task = int.parse(stdin.readLineSync());
} on Exception{
print('Please enter valid Integer value!');
continue;
}
switch(task){
case 0:
if(list.length == 0){
print('Empty List!');
}
else{
print('List:-');
list.forEach((val){
print(val);
});
}
break;
case 1:
int val = null;
print('Enter value that needs to be inserted in List:- ');
try{
val = int.parse(stdin.readLineSync());
} on Exception{
print('Please enter valid Integer value!');
continue;
}
if(val == null){
print('Failed to insert element in List!');
continue;
}
list.add(val);
list.sort();
print('Successfully inserted $val in List!');
break;
case 2:
int val = null;
print('Enter value that needs to be searched in List:- ');
try{
val = int.parse(stdin.readLineSync());
} on Exception{
print('Please enter valid Integer value!');
continue;
}
if(val == null){
print('Failed to search element in List!');
continue;
}
binarySearch(list, val);
break;
case 3:
print('Binary Search Program Complete');
break;
default:
continue;
}
}
}
/*
Sample Input/Output:
Binary Search Program
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //to add element
1
Enter value that needs to be inserted in List:-
1
Successfully inserted 1 in List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:-
1
Enter value that needs to be inserted in List:-
2
Successfully inserted 2 in List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:-
1
Enter value that needs to be inserted in List:-
3
Successfully inserted 3 in List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //to display the list
0
List:-
1
2
3
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //to search the element
2
Enter value that needs to be searched in List:-
2
Found 2 in the List at Index 1!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //quiting the program
3
Binary Search Program Complete
*/