From 46eca98c7226aa804e1116759362cabc80d16759 Mon Sep 17 00:00:00 2001 From: mavroian Date: Fri, 10 May 2019 17:43:13 +0900 Subject: [PATCH 1/3] Added Jump Search to readme.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index adcfe321b..883988d71 100644 --- a/README.md +++ b/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 From e92c4ae40a9892a55807778b7e3664420ef1f142 Mon Sep 17 00:00:00 2001 From: mavroian Date: Fri, 10 May 2019 17:43:44 +0900 Subject: [PATCH 2/3] Added JumpSearch algorithm --- Search/jumpSearch.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Search/jumpSearch.js diff --git a/Search/jumpSearch.js b/Search/jumpSearch.js new file mode 100644 index 000000000..35afb6005 --- /dev/null +++ b/Search/jumpSearch.js @@ -0,0 +1,37 @@ +/* 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); From 695ac54df2b80121f43b86adbc764f7cc61b4755 Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Sat, 11 May 2019 10:13:32 +0800 Subject: [PATCH 3/3] Update jumpSearch.js --- Search/jumpSearch.js | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Search/jumpSearch.js b/Search/jumpSearch.js index 35afb6005..e9faefca5 100644 --- a/Search/jumpSearch.js +++ b/Search/jumpSearch.js @@ -5,31 +5,29 @@ * 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 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; + + const upperBound = Math.min(step, length); + while (arr[lowerBound] < value) { + lowerBound++; + if (lowerBound === upperBound) { + return -1; + } } - } - if (arr[lowerBound] === value){ - return lowerBound; - } - 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);