mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
15
README.md
15
README.md
@ -116,6 +116,18 @@ __Properties__
|
||||
* Average case performance O(log n)
|
||||
* Worst case space complexity O(1)
|
||||
|
||||
### Jump
|
||||
![alt-text][jump-image]
|
||||
|
||||
From [Wikipedia][jump-wiki]: Jump search or block search refers to a search algorithm for ordered lists. It works by first checking all items Lkm, where {\displaystyle k\in \mathbb {N} } k\in \mathbb {N} and m is the block size, until an item is found that is larger than the search key. To find the exact position of the search key in the list a linear search is performed on the sublist L[(k-1)m, km].
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n)
|
||||
* Best case performance O(√n)
|
||||
* Average case performance O(√n)
|
||||
* Worst case space complexity O(1)
|
||||
|
||||
|
||||
----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
## Ciphers
|
||||
@ -177,5 +189,8 @@ The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" a
|
||||
[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm
|
||||
[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png
|
||||
|
||||
[jump-wiki]: https://en.wikipedia.org/wiki/Jump_search
|
||||
[jump-image]: https://i1.wp.com/theoryofprogramming.com/wp-content/uploads/2016/11/jump-search-1.jpg
|
||||
|
||||
|
||||
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg
|
||||
|
35
Search/jumpSearch.js
Normal file
35
Search/jumpSearch.js
Normal file
@ -0,0 +1,35 @@
|
||||
/* The Jump Search algorithm allows to combine a linear search with a speed optimization.
|
||||
* This means that instead of going 1 by 1, we will increase the step of √n and increase that
|
||||
* step of √n which make the step getting bigger and bigger.
|
||||
* The asymptotic analysis of Jump Search is o(√n). Like the binary search, it needs to be sorted.
|
||||
* The advantage against binary search is that Jump Search traversed back only once.
|
||||
*/
|
||||
|
||||
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) {
|
||||
lowerBound = step;
|
||||
step += step;
|
||||
if (lowerBound >= length) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
const upperBound = Math.min(step, length);
|
||||
while (arr[lowerBound] < value) {
|
||||
lowerBound++;
|
||||
if (lowerBound === upperBound) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (arr[lowerBound] === value) {
|
||||
return lowerBound;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
const arr = [0,0,4,7,10,23,34,40,55,68,77,90]
|
||||
jumpSearch(arr,4);
|
||||
jumpSearch(arr,34);
|
||||
jumpSearch(arr,77);
|
Reference in New Issue
Block a user