Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 895b353

Browse files
Jump Search
1 parent 5dae261 commit 895b353

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎05-Searching/JumpSearch.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<iostream>
2+
#include<cmath>
3+
using namespace std;
4+
5+
int jumpSearch( int arr[], int L, int key)
6+
{
7+
int step = sqrt(L);
8+
int left = 0;
9+
int right = step;
10+
11+
while(arr[right]<= key && right <= L)
12+
{
13+
left = right;
14+
right = right + step;
15+
16+
if( right > L-1)
17+
{
18+
right = L-1;
19+
}
20+
}
21+
22+
23+
for( int i = left; i <= right; i++)
24+
{
25+
if(arr[i] == key)
26+
{
27+
return i;
28+
}
29+
}
30+
return -1;
31+
}
32+
33+
int main()
34+
{
35+
int arr[] = {5,10,15,20,25,30,35,40,45,50,55,60,65,70};
36+
int L = sizeof(arr)/ sizeof(arr[0]);
37+
38+
cout<<"Enter the number to be searched"<<endl;
39+
40+
int key;
41+
cin>> key;
42+
43+
int result = jumpSearch( arr , L, key);
44+
45+
cout<< result;
46+
return 0;
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /