Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 40 additions & 75 deletions algorithms/C/searching/Jump-search.c
Original file line number Diff line number Diff line change
@@ -1,83 +1,48 @@
/*
Program to search data in array using Jump Search
Jump Search: It is a way to search data in array where it jump sqrt(n-1) and if data is less
than current then it started searching it linearly
*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include <stdio.h>
#include <math.h>

//Function to search data linearly
int jump_search(int *arr,int n,int jump,int to_search)
{
int start=0,end=jump;
//until n(size) is greater than end run the loop
while(end<n)
{
//if the data to search is greater than the end of arr then assign end to start and end+jump in end
if(arr[end]<to_search)
{
start=end;
end=end+jump;
}
// else means the data is less then or equal to the data to search
else
{
//search linearly until end is greater or equal to start
while(start<=end)
{
//if the data is found then return that data index else add 1 in start and repeat the loop
if(arr[start]==to_search)
return start;
start++;
}
}
int jumpSearch(int arr[], int n, int x) {
int step = sqrt(n);
int prev = 0;

while (arr[(int)fmin(step, n) - 1] < x) {
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}
//if data not found then return -1 will tells that data didn't exist
return -1;
}
//driver code
int main()
{
int n,*arr,jump,i,to_search,res;
printf("Enter size: ");
//taking the size of array
scanf("%d",&n);
//dynamically allocating memory and returning the base address and assign it to arr
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);

while (arr[prev] < x) {
prev++;
if (prev == fmin(step, n))
return -1;
}
//find the number of jumps to take every time
jump=(int)sqrt(n-1);
printf("Enter the number to search: ");
//taking the data to search
scanf("%d",&to_search);
//calling jump function
res=jump_search(arr,n,jump,to_search);
// if res is equal to -1 that means data didn't exist
if(res==-1)
printf("Data Not Found \n");
else
printf("Data Found at index: %d",res);
return 0;


if (arr[prev] == x)
return prev;

return -1;
}
/*
Case 1:
Input: Enter size: 10
1,2,3,4,5,6,7,8,9,10
Enter the data to search: 11
Output: Data not Found

Case 2:
int main() {
int arr[100], n, target;

printf("Enter the number of elements in the array: ");
scanf("%d", &n);

Input: Enter size: 10
1,2,3,4,5,6,7,8,9,10
Enter the data to search: 7
Output: Data Found at index: 6
printf("Enter the elements of the array in sorted order:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

Time complexity: O(squareroot (n))
printf("Enter the target value to search for: ");
scanf("%d", &target);

*/
int index = jumpSearch(arr, n, target);
if (index != -1)
printf("Element %d is present at index %d.\n", target, index);
else
printf("Element %d is not present in the array.\n", target);

return 0;
}