-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
jumpSearch.cpp
51 lines (49 loc) · 1.08 KB
/
jumpSearch.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
#include<bits/stdc++.h> //you can also use #include<iostream.h> but then also include math.h
using namespace std;
#define MAX 500
//function to perfrom jump search
int jumpSearch(int array[],int x,int n)
{
//Finding block size to be jumped
int st=sqrt(n);
//Finding the block where the element is present
int prev=0;
while(array[min(st,n)-1]<x)
{
prev=st;
st+=sqrt(n);
if(prev>=n)
return -1;
}
//Doing liner search to find the element
while(array[prev]<x)
{
prev++;
if(prev==min(st,n)) // if we reach the next block
return -1;
}
//else if the element is found
if(array[prev]==x)
{
return prev;
}
return -1;
}
int main()
{
int arr[MAX];
cout<<"Enter the number of array elements: ";
int n,search;
cin>>n;
for(int i=0;i<n;i++) //Taking input to array
cin>>arr[i];
cout<<"Enter the element to be searched: ";
cin>>search;
int index=jumpSearch(arr,search,n);
//print the index where the element is located
if(index==-1)
cout<<"Element not found";
else
cout<<"The element is at index "<<index;
return 0;
}