mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Update jumpSearch.js
This commit is contained in:
@ -5,31 +5,29 @@
|
|||||||
* The advantage against binary search is that Jump Search traversed back only once.
|
* 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;
|
const length = arr.length;
|
||||||
let step = Math.floor(Math.sqrt(length));
|
let step = Math.floor(Math.sqrt(length));
|
||||||
let lowerBound = 0;
|
let lowerBound = 0;
|
||||||
while (arr[Math.min(step, length)-1] < value)
|
while (arr[Math.min(step, length) - 1] < value) {
|
||||||
{
|
lowerBound = step;
|
||||||
lowerBound = step;
|
step += step;
|
||||||
step += step;
|
if (lowerBound >= length) {
|
||||||
if (lowerBound >= length){
|
return -1;
|
||||||
return -1;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
const upperBound = Math.min(step, length);
|
||||||
const upperBound = Math.min(step, length);
|
while (arr[lowerBound] < value) {
|
||||||
while (arr[lowerBound] < value)
|
lowerBound++;
|
||||||
{
|
if (lowerBound === upperBound) {
|
||||||
lowerBound++;
|
return -1;
|
||||||
if (lowerBound === upperBound){
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
}
|
if (arr[lowerBound] === value) {
|
||||||
if (arr[lowerBound] === value){
|
return lowerBound;
|
||||||
return lowerBound;
|
}
|
||||||
}
|
return -1;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
const arr = [0,0,4,7,10,23,34,40,55,68,77,90]
|
const arr = [0,0,4,7,10,23,34,40,55,68,77,90]
|
||||||
jumpSearch(arr,4);
|
jumpSearch(arr,4);
|
||||||
|
Reference in New Issue
Block a user