enh: jump search in C and C++ (#275)

* added jump search in c and c++

* added jump search

* fix typo mistake
pull/281/head
Ujjwal 2021-04-29 03:54:09 +05:30 committed by GitHub
parent 194316cb60
commit 3bca72cfd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 144 additions and 9 deletions

View File

@ -29,6 +29,4 @@
## Searching
- [Binary Search](searching/Binary-search.c)
- [Jump Search](searching/Jump-search.c)

View File

@ -0,0 +1,83 @@
/*
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>
//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++;
}
}
}
//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]);
}
//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;
}
/*
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:
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
Time complexity: O(squareroot (n))
*/

View File

@ -1,3 +1,5 @@
// Function to search data using jump search
#include <iostream>
#include <cmath>
using namespace std;
@ -14,26 +16,78 @@ int jumpSearch(int arr[], int n, int key)
end = n; //if right exceeds then bound the range
}
for (int pos = start; pos < end; pos++)
{ //perform linear search
{ //perform linear search
if (arr[pos] == key)
return pos; //the correct position of the key
}
return -1; // if element is not found
}
//recursive jump search function to search data in an array
int recursive_jump_search(int *arr,int start,int end,int n,int to_search,int jump)
{
// if end index of n is greater than n that means data not found so return -1
if(end>n)
return -1;
// is the element to search is greater than end index and end index is less than n than we need to jump
else if(arr[end]<to_search)
{
start=end;
end=end+jump;
}
// if else block run means data is between start and end index so then search linearly
else
{
// until start is less or equal to end run the loop
while(start<=end)
{
// if data found then return the data index number
if(arr[start]==to_search)
return start;
// if data not found at arr start th index then move forward
start++;
}
// if data not found the the loop will stop and return -1
return -1;
}
// call the recursive jump search again with few changes in start and end
return recursive_jump_search(arr,start,end,n,to_search,jump);
}
int main()
{
int n, key, loct;
cout << "Enter the length of arr:\n";
cout << "Enter the length of array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of arr:\n";
cout << "Enter the elements of array:"<<endl;
for (int i = 0; i < n; i++)
cin >> arr[i]; // Taking input
cout << "Enter search key: ";
cin >> key;
if ((loct = jumpSearch(arr, n, key)) != -1) //Calling of jumpSearch function and getting a location back
//Calling of jumpSearch function and getting a location back
cout<<"Iterative Solution:"<<endl;
if ((loct = jumpSearch(arr, n, key)) != -1)
cout << "Element found at location: " << loct << endl;
else
cout << "Element does not exist in the array." << endl;
}
cout<<"Recursive solution:"<<endl;
if ((loct=recursive_jump_search(arr,0,n-1,n,key,(int)sqrt(n-1))) != -1) //Calling of recursive jump Search function and getting a location back
cout << "Element found at location: " << loct << endl;
else
cout << "Element does not exist in the array." << endl;
return 0;
}
/*
Input: Enter the length of array: 5
Enter the elements of array:
1 2 3 4 5
Enter search key: 4
Iterative Solution:
Element found at location: 3
Recursive Solution:
Element found at location: 3
Time complexity: O(square root (n))
*/