forked from HemangTheHuman/hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump_search_array.cpp
More file actions
52 lines (51 loc) · 1.2 KB
/
jump_search_array.cpp
File metadata and controls
52 lines (51 loc) · 1.2 KB
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
//it is made for a special case for finding 10.
//uncomment to take user input
#include <iostream>
using namespace std;
int linear_search(int arr[], int N, int x, int low, int high) {
int i = low;
for (; i < N; i++)
if (arr[i] == x) {
cout << x << " found at index " << i;
return i;
}
cout << "not found";
return -1;
}
int jump_search(int a[], int n, int search_key, int low, int high, int jump) {
while (low <= high) {
if (a[low] == search_key) {
cout << search_key << " found at index " << low;
return 0;
} else if (a[low] < search_key) {
low += jump;
} else if (a[low] > search_key) {
high = low - 1;
low -= jump;
linear_search(a, n, search_key, low, high);
// return 0;
} else {
low += jump;
cout << "@";
}
}
if (low > n) {
cout << search_key << " not found in the given array";
}
return 0;
}
int main() {
int n{9};
// cin >> n;
int a[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
// for (int i{}; i < n; i++) {
// cin >> a[i];
// }
int jump{2};
int low{0};
int high{8};
int search_key{10};
// cin >> low >> high >> search_key>>jump;
jump_search(a, n, search_key, low, high, jump);
return 0;
}