Skip to content

Commit 2bfe948

Browse files
authored
Merge pull request TheAlgorithms#257 from LefterisD/master
Added Interpolation and exponential search
2 parents 4cbdc9e + a47923d commit 2bfe948

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

Search/ExponentialSearch.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Exponential Search
3+
*
4+
* The algorithm consists of two stages. The first stage determines a
5+
* range in which the search key would reside if it were in the list.
6+
* In the second stage, a binary search is performed on this range.
7+
*
8+
*
9+
*
10+
*/
11+
12+
function binarySearch (arr, x, floor, ceiling) {
13+
// Middle index
14+
const mid = Math.floor((floor + ceiling) / 2)
15+
16+
// If value is at the mid position return this position
17+
if (arr[mid] === x) {
18+
return mid
19+
}
20+
21+
if (floor > ceiling) return -1
22+
23+
// If the middle element is great than the value
24+
// search the left part of the array
25+
if (arr[mid] > value) {
26+
return binarySearch(arr, value, floor, mid - 1)
27+
// If the middle element is lower than the value
28+
// search the right part of the array
29+
} else {
30+
return binarySearch(arr, value, mid + 1, ceiling)
31+
}
32+
}
33+
34+
function exponentialSearch (arr, length, value) {
35+
// If value is the first element of the array return this position
36+
if (arr[0] === value) {
37+
return 0
38+
}
39+
40+
// Find range for binary search
41+
let i = 1
42+
while (i < length && arr[i] <= value) {
43+
i = i * 2
44+
}
45+
46+
// Call binary search for the range found above
47+
return binarySearch(arr, value, i / 2, Math.min(i, length))
48+
}
49+
50+
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
51+
const value = 78
52+
const result = exponentialSearch(arr, arr.length, value)
53+
54+
if (result < 0) {
55+
console.log('Element not found')
56+
} else {
57+
console.log('Element found at position :' + result)
58+
}

Search/InterpolationSearch.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Interpolation Search
3+
*
4+
* Time Complexity:
5+
* -Best case: O(1)
6+
* -Worst case: O(n)
7+
* -O((log(log(n))) If the data are uniformly distributed
8+
*
9+
*
10+
*/
11+
12+
function interpolationSearch (arr, key) {
13+
const length = arr.length - 1
14+
let low = 0
15+
let high = length
16+
let position = -1
17+
let delta = -1
18+
19+
// Because the array is sorted the key must be between low and high
20+
while (low <= high && key >= arr[low] && key <= arr[high]) {
21+
delta = (key - arr[low]) / (arr[high] - arr[low])
22+
position = low + Math.floor((high - low) * delta)
23+
24+
// Target found return its position
25+
if (arr[position] === key) {
26+
return position
27+
}
28+
29+
// If the key is larger then it is in the upper part of the array
30+
if (arr[position] < key) {
31+
low = position + 1
32+
// If the key is smaller then it is in the lower part of the array
33+
} else {
34+
high = position - 1
35+
}
36+
}
37+
38+
return -1
39+
}
40+
41+
const arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39]
42+
43+
console.log('Found at position :' + interpolationSearch(arr, 2))
44+
console.log('Found at position :' + interpolationSearch(arr, 12))
45+
console.log('Found at position :' + interpolationSearch(arr, 1000))
46+
console.log('Found at position :' + interpolationSearch(arr, 39))

0 commit comments

Comments
 (0)
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