Skip to content

Exponential search #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions search/binary_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

export const binarySearchIterative = (
array: number[],
target: number
target: number,
start: number = 0,
end: number = array.length - 1
): number => {
if (array.length === 0) return -1

// declare pointers for the start, middle and end indices
let start = 0,
end = array.length - 1,
middle = (start + end) >> 1

// ensure the target is within the bounds of the array
if (target < array[start] || target > array[end]) return -1

// declare pointers for the middle index
let middle = (start + end) >> 1

while (array[middle] !== target && start <= end) {
// if the target is less than the middle value, move the end pointer to be middle -1 to narrow the search space
// otherwise, move the start pointer to be middle + 1
Expand Down
41 changes: 41 additions & 0 deletions search/exponential_search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { binarySearchIterative } from './binary_search'

/**
* @description Exponential search algorithm for a sorted array.
*
* The algorithm searches for a specific value in a sorted array by first finding a range
* where the value may be present and then performing a binary search within that range.
*
* Compared with binary search, exponential search can be more convenient and advantageous
* in cases where the element to be searched is closer to the beginning of the array,
* thus avoiding several comparisons that would make the search more verbose.
* Exponential search doubles the search time with each iteration.
*
* @param {number[]} array - sorted list of numbers
* @param {number} x - target number to search for
* @return {number | null} - index of the target number in the list, or null if not found
* @see [ExponentialSearch](https://www.geeksforgeeks.org/exponential-search/)
* @example exponentialSearch([1, 2, 3, 4, 5], 3) => 2
* @example exponentialSearch([10, 20, 30, 40, 50], 35) => null
*/

export const exponentialSearch = (
array: number[],
x: number
): number | null => {
const arrayLength = array.length
if (arrayLength === 0) return null

if (array[0] === x) return 0

let i = 1
while (i < arrayLength && array[i] <= x) {
i = i * 2
}

const start = Math.floor(i / 2)
const end = Math.min(i, arrayLength - 1)
const result = binarySearchIterative(array, x, start, end)

return result !== -1 ? result : null
}
19 changes: 19 additions & 0 deletions search/test/exponential_search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { exponentialSearch } from '../exponential_search'

describe('Exponential search', () => {
test.each([
[[1, 2, 3, 4, 5], 3, 2],
[[10, 20, 30, 40, 50], 35, null],
[[10, 20, 30, 40, 50], 10, 0],
[[10, 20, 30, 40, 50], 50, 4],
[[10, 20, 30, 40, 50], 60, null],
[[], 10, null],
[[1, 2, 3, 4, 5], 1, 0],
[[1, 2, 3, 4, 5], 5, 4]
])(
'of %o, searching for %o, expected %i',
(array: number[], target: number, expected: number | null) => {
expect(exponentialSearch(array, target)).toBe(expected)
}
)
})
Loading