Skip to content

Jump Search Algorithm #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 35 additions & 0 deletions Search/jumpSearch.js
Original file line number Diff line number Diff line change
@@ -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);