Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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