Fixed exponentialSearch.js and interpolationSearch.js

This commit is contained in:
LefterisD
2020-08-10 21:06:58 +03:00
parent 6dbb849911
commit a47923d210
2 changed files with 70 additions and 76 deletions

View File

@ -1,4 +1,4 @@
/* /**
* Exponential Search * Exponential Search
* *
* The algorithm consists of two stages. The first stage determines a * The algorithm consists of two stages. The first stage determines a
@ -11,51 +11,48 @@
function binarySearch (arr, x, floor, ceiling) { function binarySearch (arr, x, floor, ceiling) {
// Middle index // Middle index
let mid = Math.floor((floor + ceiling) / 2); const mid = Math.floor((floor + ceiling) / 2)
// If value is at the mid position return this position // If value is at the mid position return this position
if (arr[mid] === x) { if (arr[mid] === x) {
return mid; return mid
} }
if(floor > ceiling) return -1; if (floor > ceiling) return -1
// If the middle element is great than the value // If the middle element is great than the value
// search the left part of the array // search the left part of the array
if (arr[mid] > value) { if (arr[mid] > value) {
return binarySearch(arr, value, floor, mid - 1); return binarySearch(arr, value, floor, mid - 1)
// If the middle element is lower than the value // If the middle element is lower than the value
// search the right part of the array // search the right part of the array
} else { } else {
return binarySearch(arr, value, mid + 1, ceiling); return binarySearch(arr, value, mid + 1, ceiling)
} }
} }
function exponentialSearch (arr, length, value) { function exponentialSearch (arr, length, value) {
// If value is the first element of the array return this position // If value is the first element of the array return this position
if (arr[0] == value) { if (arr[0] === value) {
return 0; return 0
} }
// Find range for binary search // Find range for binary search
let i = 1; let i = 1
while (i < length && arr[i] <= value) { while (i < length && arr[i] <= value) {
i = i * 2; i = i * 2
} }
// Call binary search for the range found above // Call binary search for the range found above
return binarySearch(arr, value, i / 2, Math.min(i, length)); return binarySearch(arr, value, i / 2, Math.min(i, length))
} }
let arr = [2, 3, 4, 10, 40, 65 , 78 , 100]; const arr = [2, 3, 4, 10, 40, 65, 78, 100]
let value = 78; const value = 78
let result = exponentialSearch(arr, arr.length, value); const result = exponentialSearch(arr, arr.length, value)
if (result < 0) { if (result < 0) {
console.log("Element not found"); console.log('Element not found')
} else { } else {
console.log("Element found at position :" + result); console.log('Element found at position :' + result)
} }

View File

@ -1,4 +1,4 @@
/* /**
* Interpolation Search * Interpolation Search
* *
* Time Complexity: * Time Complexity:
@ -10,40 +10,37 @@
*/ */
function interpolationSearch (arr, key) { function interpolationSearch (arr, key) {
const length = arr.length - 1
let length = arr.length - 1; let low = 0
let low = 0; let high = length
let high = length; let position = -1
let position = -1; let delta = -1
let delta = -1;
// Because the array is sorted the key must be between low and high // Because the array is sorted the key must be between low and high
while (low <= high && key >= arr[low] && key <= arr[high]) { while (low <= high && key >= arr[low] && key <= arr[high]) {
delta = (key - arr[low]) / (arr[high] - arr[low])
delta = (key - arr[low]) / (arr[high] - arr[low]); position = low + Math.floor((high - low) * delta)
position = low + Math.floor((high - low) * delta);
// Target found return its position // Target found return its position
if (arr[position] === key) { if (arr[position] === key) {
return position; return position
} }
// If the key is larger then it is in the upper part of the array // If the key is larger then it is in the upper part of the array
if (arr[position] < key) { if (arr[position] < key) {
low = position + 1; low = position + 1
// If the key is smaller then it is in the lower part of the array // If the key is smaller then it is in the lower part of the array
} else { } else {
high = position - 1; 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, 2)); console.log('Found at position :' + interpolationSearch(arr, 1000))
console.log("Found at position :" + interpolationSearch(arr, 12)); console.log('Found at position :' + interpolationSearch(arr, 39))
console.log("Found at position :" + interpolationSearch(arr, 1000));
console.log("Found at position :" + interpolationSearch(arr, 39));