forked from SanjayDevTech/Code-with-love
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnikhil-1503_jumpSearch.cpp
More file actions
60 lines (53 loc) · 1.11 KB
/
Copy pathnikhil-1503_jumpSearch.cpp
File metadata and controls
60 lines (53 loc) · 1.11 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
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
int jump_search(int arr[], int n, int k)
{
int jump = sqrt(n);
int index = 0;
while (arr[jump] <= k && jump < n) {
index = jump;
jump += sqrt(n);
if (jump > n-1)
return -1;
}
for(int j=index;j<jump;j++){
if (arr[j]==k)
return j;
}
return -1;
}
int main(){
int n,key;
cout << "Enter size of the array:"<<endl;
cin >> n;
cout << "Enter the array elements:"<<endl;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the key to be searched:"<<endl;
cin >> key;
int res = jump_search(arr,n,key);
if (res != -1)
cout << key << " found at index " << res << endl;
else
cout << key << " not found in the array" << endl;
return 0;
}
/*
Sample Input/Output:
1. Enter size of the array:
5
Enter the array elements:
12 23 31 40 57 61
Enter the key to be searched:
23
Output: 23 found at index 1
2. Enter size of the array:
5
Enter the array elements:
12 23 31 40 57 61
Enter the key to be searched:
31
Output: 31 not found in the array
*/