forked from Minor-lazer/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump_search.c
More file actions
38 lines (38 loc) · 754 Bytes
/
jump_search.c
File metadata and controls
38 lines (38 loc) · 754 Bytes
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
#include <stdio.h>
#include <math.h>
#define MAXVALUE 100
int jump_search(int a[], int low, int high, int val, int n)
{
int step, i;
step = n;
for(i=0; i<step; i++)
{
if(val < a[step])
high = step - 1;
else
low = step + 1;
}
for(i=low; i<=high; i++)
{
if(a[i] == val)
return i;
}
return -1;
}
int main()
{
int arry[MAXVALUE], i, n, val, pos;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
printf("\n Enter %d elements in the array : \n",n);
for(i=0; i<n; i++)
scanf("%d",&arry[i]);
printf("\n Enter the key element that has to be search : ");
scanf("%d", &val);
pos = jump_search(arry, 0, n-1,val, n);
if(pos == -1)
printf("\n %d is not found in the array ", val);
else
printf("\n %d is found at position arry [%d]", val,pos);
return 0;
}