-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jump_Search.cpp
72 lines (68 loc) · 1.27 KB
/
Jump_Search.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
///Time complexity: O(√n)
/*
Auther : Abdullah Al Masum
*/
#include <bits/stdc++.h>
#include <math.h>
#define MAXX 50
using namespace std;
int Array[MAXX];
int length;
int Jump_Search_Algo(int Temp[], int key)
{
int left = 0;
int right = floor(sqrt(length));
while (right < length && Temp[right] <= key)
{
left = right;
right += floor(sqrt(length));
if (right > length - 1)
{
right = length;
}
}
for (int i = left; i < right; i++)
{
if (Temp[i] == key)
return i;
}
return -1;
}
int main()
{
cout << "Input the Length :" << endl;
cin >> length;
cout << "Insert values :" << endl;
int x;
for (int i = 0; i < length; i++)
{
cin >> x;
Array[i] = x;
}
cout << "Find Value : ";
int Find, Position;
cin >> Find;
Position = Jump_Search_Algo(Array, Find);
if (Position == -1)
{
cout << endl
<< "Value not found." << endl;
}
else
{
cout << endl
<< "Value Found at : " << Position << " th position." << endl;
}
return 0;
}
/*
16
0 1 2 3 4 5 8 13 21 34 55 89 144 233 377 541
0
*/
/*
///But my algo has a liability,it can not find values when length is shorter
3
6 8 4
4
*/