diff --git a/jumpSearch.cpp b/jumpSearch.cpp new file mode 100644 index 0000000..4432cbe --- /dev/null +++ b/jumpSearch.cpp @@ -0,0 +1,48 @@ +/* +Title - Jump search Algorithm +It will search an array +time complexity - O(N^0.5) +*/ + +#include +#include + +using namespace std; +int jump_Search(int a[], int n, int item) { + int i = 0; + int m = sqrt(n); //initializing block size= √(n) + + while(a[m] <= item && m < n) { + // the control will continue to jump the blocks + i = m; // shift the block + m += sqrt(n); + if(m > n - 1) // if m exceeds the array size + return -1; + } + + for(int x = i; x> n; + int arr[n]; //creating an array of size n + cout << "\n Enter items: "; + + for(int i = 0; i< n; i++) { + cin >> arr[i]; + } + + cout << "\n Enter search key to be found in the array: "; + cin >> item; + loc = jump_Search(arr, n, item); + if(loc>=0) + cout << "\n Item found at location: " << loc; + else + cout << "\n Item is not found in the list."; +}