-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab4_jump_search.c
52 lines (51 loc) · 883 Bytes
/
Lab4_jump_search.c
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
#include <stdio.h>
#include<math.h>
int jump_search(int a[],int n,int x)
{
int i=0;
int m;
m=sqrt(n);
while(i<n && x>a[i])
{
i=i+m;
}
if(i<n)
{
if(a[i]==x)
return i;
else
{
int strt = i-m+1;
int end = i-1;
for(int j= strt;j<end;j++)
{
if(x==a[j])
{
return j;
}
}
return-1;
}
}
else
{
return -1;
}
}
int main()
{
int a[10] ;
int n,x,l,r,b;
printf("enter the size of an array");
scanf("%d",&n);
printf("enter the elements\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to be seach");
scanf("%d",&x);
b = jump_search(a,n,x);
printf("%d",b);
return 0;
}