In this tutorial you will learn about the Jump Search Algorithm and its application with practical example.
Jump Search Algorithm
Jump Search Program In 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 53 54 |
#include <stdio.h> #include <math.h> int jump_search(int arr[], int search); int n; int main() { int arr[30],i,search,loc; printf("Enter the number of elements: \n"); scanf("%d",&n); printf("Enter %d integer numbers in ascending order:\n", n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("Enter a number to search:\n"); scanf("%d",&search); loc=jump_search(arr, search); if(loc==-1) { printf("%d is not present in the list.\n", search); } else { printf("%d is found at location %d.\n", search,loc); } return 0; } int jump_search(int arr[], int search){ int jump_step,prev=0; jump_step=floor(sqrt(n)); while(arr[prev]<search){ if(arr[jump_step]>search || jump_step>=n){ break; } else{ prev=jump_step; jump_step=jump_step+floor(sqrt(n)); } } while(arr[prev]<search){ prev++; } if(arr[prev]==search){ return prev+1; } else{ return -1; } } |
Output:-