From 2fb0d48d94d9b4d14b23f9ed5cfe23410d920e81 Mon Sep 17 00:00:00 2001 From: YATIN KATHURIA <47096348+Yatin-kathuria@users.noreply.github.com> Date: Fri, 26 Nov 2021 22:08:43 +0530 Subject: [PATCH] merge: Add test cases, optamization of code, and Add description of function (#848) * Add test cases, optamization of code, and Add description of function * update testcase --- Recursive/BinarySearch.js | 45 ++++++++++++++++++++--------- Recursive/test/BinarySearch.test.js | 32 ++++++++++++++++++++ 2 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 Recursive/test/BinarySearch.test.js diff --git a/Recursive/BinarySearch.js b/Recursive/BinarySearch.js index ef24dcba0..6f6e216df 100644 --- a/Recursive/BinarySearch.js +++ b/Recursive/BinarySearch.js @@ -1,19 +1,36 @@ -// https://en.wikipedia.org/wiki/Binary_search_algorithm -// Search the integer inside the sorted integers array using Binary Search Algorithm +/** + * @function BinarySearch + * @description Search the integer inside the sorted integers array using Binary Search Algorithm. + * @param {Integer[]} arr - sorted array of integers + * @param {Integer} low - The input integer + * @param {Integer} high - The input integer + * @param {Integer} searchValue - The input integer + * @return {Integer} - return index of searchValue if found else return -1. + * @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm) + */ -export const BinarySearch = (intArr, searchQuery) => { - if (searchQuery === null || searchQuery === undefined || intArr.length === 0) { - return false +const binarySearch = (arr, low = 0, high = arr.length - 1, searchValue) => { + if (high >= low) { + 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) + } + + // Else the element can only be present in right subarray + return binarySearch(arr, mid + 1, high, searchValue) } - const middleIndex = intArr.length === 1 ? 0 : Math.ceil(intArr.length / 2) - - if (intArr[middleIndex] === searchQuery) { - return true - } else if (intArr.length > 1) { - return intArr[middleIndex] < searchQuery ? BinarySearch(intArr.slice(1, middleIndex)) : BinarySearch(intArr.slice(middleIndex)) - } else { - return false - } + // We reach here when element is not present in array + return -1 } + +export { binarySearch } diff --git a/Recursive/test/BinarySearch.test.js b/Recursive/test/BinarySearch.test.js new file mode 100644 index 000000000..2efc92c2e --- /dev/null +++ b/Recursive/test/BinarySearch.test.js @@ -0,0 +1,32 @@ +import { binarySearch } from '../BinarySearch' + +describe('BinarySearch', () => { + const arr = [2, 3, 4, 10, 25, 40, 45, 60, 100, 501, 700, 755, 800, 999] + const low = 0 + const high = arr.length - 1 + + it('should return index 3 for searchValue 10', () => { + const searchValue = 10 + expect(binarySearch(arr, low, high, searchValue)).toBe(3) + }) + + it('should return index 0 for searchValue 2', () => { + const searchValue = 2 + expect(binarySearch(arr, low, high, searchValue)).toBe(0) + }) + + it('should return index 13 for searchValue 999', () => { + const searchValue = 999 + expect(binarySearch(arr, low, high, searchValue)).toBe(13) + }) + + it('should return -1 for searchValue 1', () => { + const searchValue = 1 + expect(binarySearch(arr, low, high, searchValue)).toBe(-1) + }) + + it('should return -1 for searchValue 1000', () => { + const searchValue = 1000 + expect(binarySearch(arr, low, high, searchValue)).toBe(-1) + }) +})