Skip to content

Instantly share code, notes, and snippets.

@the-mgi
Last active November 26, 2020 20:13
Show Gist options
  • Select an option

  • Save the-mgi/0e78ed7dd235e6e2441c60e05d023b27 to your computer and use it in GitHub Desktop.

Select an option

Save the-mgi/0e78ed7dd235e6e2441c60e05d023b27 to your computer and use it in GitHub Desktop.
function JumpSearchRecursive(array: number[], key: number): number {
const jumpSize = Math.floor(Math.sqrt(array.length));
let val = jumpSize;
const getHighLowRanges = () => {
if ((array[val] > key) || (val >= array.length)) {
return;
} else {
val = val + jumpSize;
getHighLowRanges();
}
};
getHighLowRanges();
let ansIndex = -1;
val = val > array.length ? array.length : val;
const iterateOverRange = (startIndex, lastIndex) => {
if (startIndex >= lastIndex) {
return -1;
} else if (array[startIndex] === key) {
return startIndex;
} else {
return iterateOverRange(startIndex + 1, lastIndex);
}
};
ansIndex = iterateOverRange(val - jumpSize, val);
return ansIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment