From 411b21201794fcc201f0a15b19b34ee0f049acd2 Mon Sep 17 00:00:00 2001 From: devanshigarg01 <75984570+devanshigarg01@users.noreply.github.com> Date: Sat, 30 Oct 2021 19:03:50 +0530 Subject: [PATCH] Jump Search --- Searching/Jump_Search/JumpSearch.cpp | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Searching/Jump_Search/JumpSearch.cpp diff --git a/Searching/Jump_Search/JumpSearch.cpp b/Searching/Jump_Search/JumpSearch.cpp new file mode 100644 index 0000000..8e6a69d --- /dev/null +++ b/Searching/Jump_Search/JumpSearch.cpp @@ -0,0 +1,47 @@ +#include +#include +using namespace std; + +int jumpSearch( int arr[], int L, int key) +{ +int step = sqrt(L); +int left = 0; +int right = step; + +while(arr[right]<= key && right <= L) +{ + left = right; + right = right + step; + + if( right > L-1) +{ + right = L-1; +} +} + + +for( int i = left; i <= right; i++) +{ + if(arr[i] == key) + { + return i; + } +} +return -1; +} + +int main() +{ + int arr[] = {5,10,15,20,25,30,35,40,45,50,55,60,65,70}; + int L = sizeof(arr)/ sizeof(arr[0]); + + cout<<"Enter the number to be searched"<> key; + + int result = jumpSearch( arr , L, key); + + cout<< result; + return 0; +}