-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpSearch.c
More file actions
38 lines (33 loc) · 932 Bytes
/
JumpSearch.c
File metadata and controls
38 lines (33 loc) · 932 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 <stdlib.h>
#include <math.h>
int Min(int a, int b);
int JumpSearch(int* arr, int num, int n)
{
int f=0,l, jump = sqrt(n);
l=f+jump;
while(l<n && arr[l]<=num)
{
f=l;
l=l+jump;
}
for(int i = Min(l-1,n-1);i >= f;i--)
if(arr[i] == num) return i;
return -1;
}
int main()
{
int arr[] = {10,12,23,34,45,56,67,78,89,90}; //Sorted Array (Ascending for this program)
int n, num, index;//n = length of array, num = number to be searched, index = index at which num resides in the array(to be calculated)
n = sizeof(arr) / sizeof(arr[0]);
printf("Enter the Number to be searched : ");
scanf("%d",&num);
index = JumpSearch (arr, num, n);
if ( index != -1) printf("%d found at index %d",num,index);
else printf("%d not found",num);
return 0;
}
int Min(int a, int b)
{
if(a<b) return a; else return b;
}