From 1e82432e81d3e843c45070fcf900087816b45f96 Mon Sep 17 00:00:00 2001 From: Michael Nguyen Date: Fri, 30 Oct 2020 22:21:37 +0700 Subject: [PATCH] Add implementation for Jump Search in JS --- JavaScript/jumpSearch.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 JavaScript/jumpSearch.js diff --git a/JavaScript/jumpSearch.js b/JavaScript/jumpSearch.js new file mode 100644 index 0000000..6a29285 --- /dev/null +++ b/JavaScript/jumpSearch.js @@ -0,0 +1,36 @@ +function jumpSearch(arr, x, n) { + // Finding block size to be jumped + let step = Math.sqrt(n); + + // Finding the block where element is present + let prev = 0; + while (arr[Math.min(step, n) - 1] < x) { + prev = step; + step += Math.sqrt(n); + if (prev >= n) { + return -1; + } + } + + // Doing a linear search for x in + // block beginning with prev + + while (arr[prev] < x) { + prev += 1; + if (prev == Math.min(step, n)) { + return -1; + } + } + + if (arr[prev] == x) { + return prev; + } + return -1; +} + +let arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 ]; +let x = 55; +let n = arr.length; + +let index = jumpSearch(arr, x, n); +console.log(`Number ${x} is at index ${index}`); \ No newline at end of file