From 3bca72cfd2db603ee3146978129be629dd8b2123 Mon Sep 17 00:00:00 2001 From: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Date: Thu, 29 Apr 2021 03:54:09 +0530 Subject: [PATCH] enh: jump search in C and C++ (#275) * added jump search in c and c++ * added jump search * fix typo mistake --- algorithms/C/README.md | 4 +- algorithms/C/searching/Jump-search.c | 83 +++++++++++++++++++ .../CPlusPlus/Searching/jump-search.cpp | 66 +++++++++++++-- 3 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 algorithms/C/searching/Jump-search.c diff --git a/algorithms/C/README.md b/algorithms/C/README.md index efa287a9..2ea6a755 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -29,6 +29,4 @@ ## Searching - [Binary Search](searching/Binary-search.c) - - - +- [Jump Search](searching/Jump-search.c) diff --git a/algorithms/C/searching/Jump-search.c b/algorithms/C/searching/Jump-search.c new file mode 100644 index 00000000..92922a68 --- /dev/null +++ b/algorithms/C/searching/Jump-search.c @@ -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 +#include +#include + +//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 #include 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]> n; int arr[n]; - cout << "Enter the elements of arr:\n"; + cout << "Enter the elements of array:"<> 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:"<