Skip to content

Commit 2c1cd55

Browse files
authored
Merge pull request #86 from Mavroian/master
Add Jump Search Algorithm
2 parents f76f615 + 695ac54 commit 2c1cd55

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ __Properties__
116116
* Average case performance O(log n)
117117
* Worst case space complexity O(1)
118118

119+
### Jump
120+
![alt-text][jump-image]
121+
122+
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].
123+
124+
__Properties__
125+
* Worst case performance  O(n)
126+
* Best case performance O(√n)
127+
* Average case performance  O(√n)
128+
* Worst case space complexity O(1)
129+
130+
119131
----------------------------------------------------------------------------------------------------------------------
120132

121133
## Ciphers
@@ -177,5 +189,8 @@ The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" a
177189
[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm
178190
[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png
179191

192+
[jump-wiki]: https://en.wikipedia.org/wiki/Jump_search
193+
[jump-image]: https://i1.wp.com/theoryofprogramming.com/wp-content/uploads/2016/11/jump-search-1.jpg
194+
180195

181196
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg

Search/jumpSearch.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* The Jump Search algorithm allows to combine a linear search with a speed optimization.
2+
* This means that instead of going 1 by 1, we will increase the step of √n and increase that
3+
* step of √n which make the step getting bigger and bigger.
4+
* The asymptotic analysis of Jump Search is o(√n). Like the binary search, it needs to be sorted.
5+
* The advantage against binary search is that Jump Search traversed back only once.
6+
*/
7+
8+
const jumpSearch = (arr, value) => {
9+
const length = arr.length;
10+
let step = Math.floor(Math.sqrt(length));
11+
let lowerBound = 0;
12+
while (arr[Math.min(step, length) - 1] < value) {
13+
lowerBound = step;
14+
step += step;
15+
if (lowerBound >= length) {
16+
return -1;
17+
}
18+
}
19+
20+
const upperBound = Math.min(step, length);
21+
while (arr[lowerBound] < value) {
22+
lowerBound++;
23+
if (lowerBound === upperBound) {
24+
return -1;
25+
}
26+
}
27+
if (arr[lowerBound] === value) {
28+
return lowerBound;
29+
}
30+
return -1;
31+
}
32+
const arr = [0,0,4,7,10,23,34,40,55,68,77,90]
33+
jumpSearch(arr,4);
34+
jumpSearch(arr,34);
35+
jumpSearch(arr,77);

0 commit comments

Comments
 (0)