-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenudrivensearching.cpp
104 lines (90 loc) · 1.64 KB
/
menudrivensearching.cpp
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
#include<iostream>
using namespace std;
class searching
{
int size;
public:
void input();
int linear();
int binary();
void display();
};
void searching:: input()
{
int arr[10], key;
do
{
cout << "Enter size (size must be between 1-10): ";
cin >> size;
if (size > 10 || size < 1)
cout << " You entered wrong size...\nEnter again...\n";
}
while (size > 10 || size < 1);
cout << "Enter sorted array elements :";
for (int i = 0;i < size;i++)
cin >> arr[i];
cout<<" Enter array element to be searched: ";
cin>>key;
}
void searching:: linear()
{
int i;
int arr[10], key;
for(i = 0; i < size; i++)
{
if(arr[i] == key)
return i;
}
}
void searching:: binary()
{
int low, high, mid, key, arr[10];
low = 0;
high = size - 1;
while(low <= high)
{
mid = (low + high)/2;
if(key == arr[mid])
return[mid];
if(key < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
void searching:: display()
{
int i,key;
cout<<" Array element "<<key<<" found at: "<<mid;
}
int main()
{
searching s;
int choice;
char ch;
do
{
s.input();
cout << "Menu\n1.Linear Search\n2.Binary Search";
cout << "\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
s.linear();
s.display();
break;
case 2:
s.binary();
s.display();
break;
default:
cout << "\nWrong input...\nEnter correct choice...";
}
cout << "\nDo you want to continue?(Y/N)?";
cin >> ch;
}
while (ch == 'y' || ch == 'Y');
return 0;
}