Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 2
You multiply the index “right” by 2, without checking it on out of array bounds. Outside the array there is only garbage which of course is not ordered. These algorithms work only on ordered arrays.
You should check index "right" on array size exceeding like this:
int JumpSearch(int x)
{
constexpr int size = sizeof(V)/sizeof(V[0]);
int left = 0;
int right = sqrt(size);
while (left < right){
if (V[right] == x)
return right;
if (V[right] > x)
return BinarySearch(x,left,right);
if (V[right] < x)
left = right;
right = right * 2;
if(right >= size) right = size - 1;
}
return -1;
}
Also your JumpSearch() function didn't have a return statament at the end of the definition.