Created
March 3, 2019 03:13
-
-
Save graphoarty/fc6d09b8e9a2a60a1e08f8b56e5276ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int jumpSearch(int L[], int length, int key){ | |
int left = 0; | |
int right = sqrt(length); | |
while(right < length && L[right] <= key){ | |
left = right; | |
right += sqrt(length); | |
if(right > length - 1){ | |
right = length; | |
} | |
} | |
for(int i = left; i < right; i++){ | |
if(L[i] == key){ | |
return i; | |
} | |
} | |
return -1; | |
} | |
int main() | |
{ | |
int L[] = {0, 1, 2, 3, 4, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; | |
int length = sizeof(L) / sizeof(L[0]); | |
int key = 34; | |
int x; | |
if((x = jumpSearch(L, length, key)) == -1 ){ | |
cout << "Key doesn't exist" << endl; | |
} else { | |
cout << "The position of Key is " << x << endl; | |
} | |
return 0; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment