Skip to content

Commit 2c6a281

Browse files
committed
fix: modified the return on binary search and related tests
1 parent 6d55e80 commit 2c6a281

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

search/binary_search.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
*
99
* @param {number[]} array - sorted list of numbers
1010
* @param {number} target - target number to search for
11-
* @return {number} - index of the target number in the list, or -1 if not found
11+
* @return {number} - index of the target number in the list, or null if not found
1212
* @see [BinarySearch](https://www.geeksforgeeks.org/binary-search/)
1313
* @example binarySearch([1,2,3], 2) => 1
14-
* @example binarySearch([4,5,6], 2) => -1
14+
* @example binarySearch([4,5,6], 2) => null
1515
*/
1616

1717
export const binarySearchIterative = (
1818
array: number[],
1919
target: number,
2020
start: number = 0,
2121
end: number = array.length - 1
22-
): number => {
23-
if (array.length === 0) return -1
22+
): number | null => {
23+
if (array.length === 0) return null
2424

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

2828
// declare pointers for the middle index
2929
let middle = (start + end) >> 1
@@ -37,24 +37,24 @@ export const binarySearchIterative = (
3737
middle = (start + end) >> 1
3838
}
3939
// return the middle index if it is equal to target
40-
return array[middle] === target ? middle : -1
40+
return array[middle] === target ? middle : null
4141
}
4242

4343
export const binarySearchRecursive = (
4444
array: number[],
4545
target: number,
4646
start = 0,
4747
end = array.length - 1
48-
): number => {
49-
if (array.length === 0) return -1
48+
): number | null => {
49+
if (array.length === 0) return null
5050

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

5454
const middle = (start + end) >> 1
5555

5656
if (array[middle] === target) return middle // target found
57-
if (start > end) return -1 // target not found
57+
if (start > end) return null // target not found
5858

5959
// if the target is less than the middle value, move the end pointer to be middle -1 to narrow the search space
6060
// otherwise, move the start pointer to be middle + 1

search/exponential_search.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { binarySearchIterative } from './binary_search'
99
* Compared with binary search, exponential search can be more convenient and advantageous
1010
* in cases where the element to be searched is closer to the beginning of the array,
1111
* thus avoiding several comparisons that would make the search more verbose.
12-
* Exponential search doubles the search time with each iteration.
1312
*
1413
* @param {number[]} array - sorted list of numbers
1514
* @param {number} x - target number to search for
@@ -37,5 +36,5 @@ export const exponentialSearch = (
3736
const end = Math.min(i, arrayLength - 1)
3837
const result = binarySearchIterative(array, x, start, end)
3938

40-
return result !== -1 ? result : null
39+
return result
4140
}

search/test/binary_search.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { binarySearchIterative, binarySearchRecursive } from '../binary_search'
22

33
describe('BinarySearch', () => {
44
const testArray: number[] = [1, 2, 3, 4]
5-
type FunctionsArray = { (array: number[], index: number): number }[]
5+
type FunctionsArray = { (array: number[], index: number): number | null }[]
66
const functions: FunctionsArray = [
77
binarySearchIterative,
88
binarySearchRecursive
@@ -12,14 +12,16 @@ describe('BinarySearch', () => {
1212
it('should be defined', () => {
1313
expect(func(testArray, 2)).toBeDefined()
1414
})
15-
it('should return a number', () => {
16-
expect(typeof func(testArray, 2)).toBe('number')
15+
it('should return a number or null', () => {
16+
expect(
17+
typeof func(testArray, 2) === 'number' || func(testArray, 2) === null
18+
).toBe(true)
1719
})
18-
it('should return -1 if the target is not found in the array', () => {
19-
expect(func(testArray, 5)).toBe(-1)
20+
it('should return null if the target is not found in the array', () => {
21+
expect(func(testArray, 5)).toBe(null)
2022
})
21-
it('should return -1 if there are no elements in the array', () => {
22-
expect(func([], 5)).toBe(-1)
23+
it('should return null if there are no elements in the array', () => {
24+
expect(func([], 5)).toBe(null)
2325
})
2426
it('should return the index of the target if it is found in the array', () => {
2527
expect(func(testArray, 2)).toBe(1)

0 commit comments

Comments
 (0)