Skip to content

binarySearch #884

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 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 16 additions & 17 deletions Recursive/BinarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@
* @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm)
*/

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

const mid = low + Math.floor((high - low) / 2)

// If the element is present at the middle
if (arr[mid] === searchValue) {
return mid
}

// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > searchValue) {
return binarySearch(arr, low, mid - 1, searchValue)
}
// If the element is present at the middle
if (arr[mid] === searchValue) {
return mid
}

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

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

}

export { binarySearch }
10 changes: 5 additions & 5 deletions Recursive/test/BinarySearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ describe('BinarySearch', () => {

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

it('should return index 0 for searchValue 2', () => {
const searchValue = 2
expect(binarySearch(arr, low, high, searchValue)).toBe(0)
expect(binarySearch(arr, searchValue, low, high)).toBe(0)
})

it('should return index 13 for searchValue 999', () => {
const searchValue = 999
expect(binarySearch(arr, low, high, searchValue)).toBe(13)
expect(binarySearch(arr, searchValue, low, high)).toBe(13)
})

it('should return -1 for searchValue 1', () => {
const searchValue = 1
expect(binarySearch(arr, low, high, searchValue)).toBe(-1)
expect(binarySearch(arr, searchValue, low, high)).toBe(-1)
})

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