Update jumpSearch.js

This commit is contained in:
Libin Yang
2019-05-11 10:13:32 +08:00
committed by GitHub
parent e92c4ae40a
commit 695ac54df2

View File

@ -5,28 +5,26 @@
* The advantage against binary search is that Jump Search traversed back only once.
*/
const jumpSearch = (arr, value)=>{
const jumpSearch = (arr, value) => {
const length = arr.length;
let step = Math.floor(Math.sqrt(length));
let lowerBound = 0;
while (arr[Math.min(step, length)-1] < value)
{
while (arr[Math.min(step, length) - 1] < value) {
lowerBound = step;
step += step;
if (lowerBound >= length){
if (lowerBound >= length) {
return -1;
}
}
const upperBound = Math.min(step, length);
while (arr[lowerBound] < value)
{
while (arr[lowerBound] < value) {
lowerBound++;
if (lowerBound === upperBound){
if (lowerBound === upperBound) {
return -1;
}
}
if (arr[lowerBound] === value){
if (arr[lowerBound] === value) {
return lowerBound;
}
return -1;