From 6dbb849911170b0651dab0aabca9fa5a36c807f5 Mon Sep 17 00:00:00 2001 From: LefterisD Date: Mon, 10 Aug 2020 18:03:01 +0300 Subject: [PATCH 1/2] Added Interpolation and exponential searches in the javascript repo --- Search/ExponentialSearch.js | 61 +++++++++++++++++++++++++++++++++++ Search/InterpolationSearch.js | 49 ++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 Search/ExponentialSearch.js create mode 100644 Search/InterpolationSearch.js diff --git a/Search/ExponentialSearch.js b/Search/ExponentialSearch.js new file mode 100644 index 0000000000..d866ed5d8e --- /dev/null +++ b/Search/ExponentialSearch.js @@ -0,0 +1,61 @@ +/* + * Exponential Search + * + * The algorithm consists of two stages. The first stage determines a + * range in which the search key would reside if it were in the list. + * In the second stage, a binary search is performed on this range. + * + * + * + */ + +function binarySearch(arr, x, floor, ceiling) { + // Middle index + let mid = Math.floor((floor + ceiling) / 2); + + // If value is at the mid position return this position + if (arr[mid] === x) { + return mid; + } + + if(floor > ceiling) return -1; + + // If the middle element is great than the value + // search the left part of the array + if (arr[mid] > value) { + return binarySearch(arr, value, floor, mid - 1); + //If the middle element is lower than the value + //search the right part of the array + } else { + return binarySearch(arr, value, mid + 1, ceiling); + } + + +} + + +function exponentialSearch(arr, length, value) { + // If value is the first element of the array return this position + if (arr[0] == value) { + return 0; + } + + // Find range for binary search + let i = 1; + while (i < length && arr[i] <= value) { + i = i * 2; + } + + // Call binary search for the range found above + return binarySearch(arr, value, i / 2, Math.min(i, length)); +} + +let arr = [2, 3, 4, 10, 40, 65 , 78 , 100]; +let value = 78; +let result = exponentialSearch(arr, arr.length, value); + +if (result < 0) { + console.log("Element not found"); +} else { + console.log("Element found at position :" + result); +} diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js new file mode 100644 index 0000000000..f9a54bf605 --- /dev/null +++ b/Search/InterpolationSearch.js @@ -0,0 +1,49 @@ +/* + * Interpolation Search + * + * Time Complexity: + * -Best case: O(1) + * -Worst case: O(n) + * -O((log(log(n))) If the data are uniformly distributed + * + * + */ + +function interpolationSearch(arr, key) { + + let length = arr.length - 1; + let low = 0; + let high = length; + let position = -1; + let delta = -1; + + //Because the array is sorted the key must be between low and high + while (low <= high && key >= arr[low] && key <= arr[high]) { + + delta = (key - arr[low]) / (arr[high] - arr[low]); + position = low + Math.floor((high - low) * delta); + + //Target found return its position + if (arr[position] === key) { + return position; + } + + //If the key is larger then it is in the upper part of the array + if (arr[position] < key) { + low = position + 1; + //If the key is smaller then it is in the lower part of the array + } else { + high = position - 1; + } + } + + return -1; +} + + +let arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39]; + +console.log("Found at position :" + interpolationSearch(arr, 2)); +console.log("Found at position :" + interpolationSearch(arr, 12)); +console.log("Found at position :" + interpolationSearch(arr, 1000)); +console.log("Found at position :" + interpolationSearch(arr, 39)); \ No newline at end of file From a47923d210a5618f6f6cc563ec02befe8ba0951b Mon Sep 17 00:00:00 2001 From: LefterisD Date: Mon, 10 Aug 2020 21:06:58 +0300 Subject: [PATCH 2/2] Fixed exponentialSearch.js and interpolationSearch.js --- Search/ExponentialSearch.js | 87 +++++++++++++++++------------------ Search/InterpolationSearch.js | 67 +++++++++++++-------------- 2 files changed, 74 insertions(+), 80 deletions(-) diff --git a/Search/ExponentialSearch.js b/Search/ExponentialSearch.js index d866ed5d8e..e46a5ee89e 100644 --- a/Search/ExponentialSearch.js +++ b/Search/ExponentialSearch.js @@ -1,61 +1,58 @@ -/* +/** * Exponential Search * - * The algorithm consists of two stages. The first stage determines a - * range in which the search key would reside if it were in the list. + * The algorithm consists of two stages. The first stage determines a + * range in which the search key would reside if it were in the list. * In the second stage, a binary search is performed on this range. - * * - * + * + * */ -function binarySearch(arr, x, floor, ceiling) { - // Middle index - let mid = Math.floor((floor + ceiling) / 2); - - // If value is at the mid position return this position - if (arr[mid] === x) { - return mid; - } - - if(floor > ceiling) return -1; - - // If the middle element is great than the value - // search the left part of the array - if (arr[mid] > value) { - return binarySearch(arr, value, floor, mid - 1); - //If the middle element is lower than the value - //search the right part of the array - } else { - return binarySearch(arr, value, mid + 1, ceiling); - } - - +function binarySearch (arr, x, floor, ceiling) { + // Middle index + const mid = Math.floor((floor + ceiling) / 2) + + // If value is at the mid position return this position + if (arr[mid] === x) { + return mid + } + + if (floor > ceiling) return -1 + + // If the middle element is great than the value + // search the left part of the array + if (arr[mid] > value) { + return binarySearch(arr, value, floor, mid - 1) + // If the middle element is lower than the value + // search the right part of the array + } else { + return binarySearch(arr, value, mid + 1, ceiling) + } } +function exponentialSearch (arr, length, value) { + // If value is the first element of the array return this position + if (arr[0] === value) { + return 0 + } -function exponentialSearch(arr, length, value) { - // If value is the first element of the array return this position - if (arr[0] == value) { - return 0; - } - - // Find range for binary search - let i = 1; - while (i < length && arr[i] <= value) { - i = i * 2; - } + // Find range for binary search + let i = 1 + while (i < length && arr[i] <= value) { + i = i * 2 + } - // Call binary search for the range found above - return binarySearch(arr, value, i / 2, Math.min(i, length)); + // Call binary search for the range found above + return binarySearch(arr, value, i / 2, Math.min(i, length)) } -let arr = [2, 3, 4, 10, 40, 65 , 78 , 100]; -let value = 78; -let result = exponentialSearch(arr, arr.length, value); +const arr = [2, 3, 4, 10, 40, 65, 78, 100] +const value = 78 +const result = exponentialSearch(arr, arr.length, value) if (result < 0) { - console.log("Element not found"); + console.log('Element not found') } else { - console.log("Element found at position :" + result); + console.log('Element found at position :' + result) } diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index f9a54bf605..c256fd8c7e 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -1,49 +1,46 @@ -/* +/** * Interpolation Search - * + * * Time Complexity: * -Best case: O(1) * -Worst case: O(n) * -O((log(log(n))) If the data are uniformly distributed * - * + * */ -function interpolationSearch(arr, key) { - - let length = arr.length - 1; - let low = 0; - let high = length; - let position = -1; - let delta = -1; - - //Because the array is sorted the key must be between low and high - while (low <= high && key >= arr[low] && key <= arr[high]) { - - delta = (key - arr[low]) / (arr[high] - arr[low]); - position = low + Math.floor((high - low) * delta); - - //Target found return its position - if (arr[position] === key) { - return position; - } +function interpolationSearch (arr, key) { + const length = arr.length - 1 + let low = 0 + let high = length + let position = -1 + let delta = -1 + + // Because the array is sorted the key must be between low and high + while (low <= high && key >= arr[low] && key <= arr[high]) { + delta = (key - arr[low]) / (arr[high] - arr[low]) + position = low + Math.floor((high - low) * delta) + + // Target found return its position + if (arr[position] === key) { + return position + } - //If the key is larger then it is in the upper part of the array - if (arr[position] < key) { - low = position + 1; - //If the key is smaller then it is in the lower part of the array - } else { - high = position - 1; - } + // If the key is larger then it is in the upper part of the array + if (arr[position] < key) { + low = position + 1 + // If the key is smaller then it is in the lower part of the array + } else { + high = position - 1 } + } - return -1; + return -1 } +const arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39] -let arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39]; - -console.log("Found at position :" + interpolationSearch(arr, 2)); -console.log("Found at position :" + interpolationSearch(arr, 12)); -console.log("Found at position :" + interpolationSearch(arr, 1000)); -console.log("Found at position :" + interpolationSearch(arr, 39)); \ No newline at end of file +console.log('Found at position :' + interpolationSearch(arr, 2)) +console.log('Found at position :' + interpolationSearch(arr, 12)) +console.log('Found at position :' + interpolationSearch(arr, 1000)) +console.log('Found at position :' + interpolationSearch(arr, 39)) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy