-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch_Linear_Sentinel_Binary_Fibonacci.cpp
142 lines (131 loc) · 3.95 KB
/
Search_Linear_Sentinel_Binary_Fibonacci.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
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
#include<iostream>
using namespace std;
class search {
public:
int n, i, data[50], son[50], sortdata[50], s;
void intake() {
cout << "\nEnter no. of elements: ";
cin >> n;
cout << "\nEnter Elements:\n";
for (i = 1; i <= n; i++) {
cin >> data[i];
son[i] = data[i];
}
}
void search_lini(int item) {
s = 0;
for (i = 1; i <= n; i++) {
if (data[i] == item) {
s = 1;
break;
}
}
if (s == 1)
cout << "\nAttended training program.";
else
cout << "\nDidn't attended training program.";
}
void search_senti(int a) {
int i = 0;
while (data[i] != a && i < n) { i++; }
if (i < n)
cout << "\n" << a << " attended training program.";
else
cout << "\n" << a << " didn't attend training program.";
}
void search_bin(int x) {
int y = 0;
int left, right, mid;
left = 0;
right = n - 1;
while (left <= right) {
mid = (left + right) / 2;
if (data[mid] == x) {
y = 1;
break;
} else if (data[mid] > x)
right = mid - 1;
else if (data[mid] < x)
left = mid + 1;
}
if (y == 1) { cout << "\n" << x << " attended training program."; }
else { cout << "\n" << x << " didn't attend training program."; }
}
int fibsearch(int x) {
int inf = 0, pos, k;
static int kk = -1, nn = -1;
static int fib[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 98,
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811,
514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817,
39088169, 63245986, 102334155, 165580141};
if (nn != n) {
k = 0;
while (fib[k] < n)
k++;
kk = k;
nn = n;
} else
k = kk;
while (k > 0) {
pos = inf + fib[--k];
if ((pos >= n) || (x < data[pos]));
else if (x > data[pos]) {
inf = pos + 1;
k--;
} else
return pos;
}
return -1;
}
void sort() {
int p, j, temp;
for (p = 1; p <= n - 1; p++) {
for (j = 1; j <= n - 1; j++) {
if (data[j] > data[j + 1]) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
};
int main() {
int s, r, pos;
char y = 'y';
search t;
t.intake();
t.sort();
while (y == 'y') {
cout << "\n==============================Searching Techniques==============================";
cout << "\n1. Linear search.\n2. Sentinel search.\n3. Binary search.\n4. Fibonacci search";
cin >> s;
cout << "\nEnter roll no. to search:";
cin >> r;
switch (s) {
case 1:
cout << "\nUsing linear search:\n";
t.search_lini(r);
break;
case 3:
cout << "\nUsing binary search:\n";
t.search_bin(r);
break;
case 2:
cout << "\nUsing sentinel search:\n";
t.search_senti(r);
break;
case 4:
cout << "\nUsing fibonacci search:\n";
pos = t.fibsearch(r);
if (pos >= 0)
cout << "\nAttended the training program." << endl;
else
cout << "\nDidn't attended training program." << endl;
break;
}
cout << "\nDo you want to continue? (y/n): ";
cin >> y;
}
return 0;
}