Skip to content

Commit 833d05d

Browse files
authored
merge: binarySearch (#884)
* required, optional param left-to-right approch in BS * base case return early to avoid nested if block * codes formated with standard.js
1 parent c496925 commit 833d05d

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

Recursive/BinarySearch.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,25 @@
1010
* @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm)
1111
*/
1212

13-
const binarySearch = (arr, low = 0, high = arr.length - 1, searchValue) => {
14-
if (high >= low) {
15-
const mid = low + Math.floor((high - low) / 2)
13+
const binarySearch = (arr, searchValue, low = 0, high = arr.length - 1) => {
14+
// base case
15+
if (high < low || arr.length === 0) return -1
1616

17-
// If the element is present at the middle
18-
if (arr[mid] === searchValue) {
19-
return mid
20-
}
17+
const mid = low + Math.floor((high - low) / 2)
2118

22-
// If element is smaller than mid, then
23-
// it can only be present in left subarray
24-
if (arr[mid] > searchValue) {
25-
return binarySearch(arr, low, mid - 1, searchValue)
26-
}
19+
// If the element is present at the middle
20+
if (arr[mid] === searchValue) {
21+
return mid
22+
}
2723

28-
// Else the element can only be present in right subarray
29-
return binarySearch(arr, mid + 1, high, searchValue)
24+
// If element is smaller than mid, then
25+
// it can only be present in left subarray
26+
if (arr[mid] > searchValue) {
27+
return binarySearch(arr, searchValue, low, mid - 1)
3028
}
3129

32-
// We reach here when element is not present in array
33-
return -1
30+
// Else the element can only be present in right subarray
31+
return binarySearch(arr, searchValue, mid + 1, high)
3432
}
3533

3634
export { binarySearch }

Recursive/test/BinarySearch.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ describe('BinarySearch', () => {
77

88
it('should return index 3 for searchValue 10', () => {
99
const searchValue = 10
10-
expect(binarySearch(arr, low, high, searchValue)).toBe(3)
10+
expect(binarySearch(arr, searchValue, low, high)).toBe(3)
1111
})
1212

1313
it('should return index 0 for searchValue 2', () => {
1414
const searchValue = 2
15-
expect(binarySearch(arr, low, high, searchValue)).toBe(0)
15+
expect(binarySearch(arr, searchValue, low, high)).toBe(0)
1616
})
1717

1818
it('should return index 13 for searchValue 999', () => {
1919
const searchValue = 999
20-
expect(binarySearch(arr, low, high, searchValue)).toBe(13)
20+
expect(binarySearch(arr, searchValue, low, high)).toBe(13)
2121
})
2222

2323
it('should return -1 for searchValue 1', () => {
2424
const searchValue = 1
25-
expect(binarySearch(arr, low, high, searchValue)).toBe(-1)
25+
expect(binarySearch(arr, searchValue, low, high)).toBe(-1)
2626
})
2727

2828
it('should return -1 for searchValue 1000', () => {
2929
const searchValue = 1000
30-
expect(binarySearch(arr, low, high, searchValue)).toBe(-1)
30+
expect(binarySearch(arr, searchValue, low, high)).toBe(-1)
3131
})
3232
})

0 commit comments

Comments
 (0)