-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add exponential search * test: add test for exponential search * fix : prettier fix * fix: added parameters to iterative binary search * fix: prettier fix #2 * fix: modified the return on binary search and related tests
- Loading branch information
Showing
4 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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. | ||
* | ||
* @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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
}) |