Skip to content

Commit e413550

Browse files
docs: Improve comments and documentation in binary search algorithm
2 parents 08d8c6b + f44a7f3 commit e413550

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Recursive/BinarySearch.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
/**
2-
* @function BinarySearch
3-
* @description Search the integer inside the sorted integers array using Binary Search Algorithm.
4-
* @param {Integer[]} arr - sorted array of integers
5-
* @param {Integer} low - The input integer
6-
* @param {Integer} high - The input integer
7-
* @param {Integer} searchValue - The input integer
8-
* @return {Integer} - return index of searchValue if found else return -1.
2+
* @function binarySearch
3+
* @description Recursively searches for a `searchValue` within a sorted `arr` of integers.
4+
* This implementation uses default parameters for `low` and `high` to allow
5+
* for a simple initial call (e.g., `binarySearch(arr, value)`).
6+
*
7+
* @example
8+
* const arr = [1, 2, 3, 4, 5, 6, 7];
9+
* const value = 5;
10+
* const index = binarySearch(arr, value);
11+
* // index will be 4
12+
*
13+
* @example
14+
* const arr = [1, 2, 3, 4, 5, 6, 7];
15+
* const value = 8;
16+
* const index = binarySearch(arr, value);
17+
* // index will be -1
18+
*
19+
* @param {Integer[]} arr - The sorted array of integers to search.
20+
* @param {Integer} searchValue - The integer value to search for in the array.
21+
* @param {Integer} [low=0] - The starting index of the subarray. (Primarily for internal recursive use).
22+
* @param {Integer} [high=arr.length-1] - The ending index of the subarray. (Primarily for internal recursive use).
23+
* @return {Integer} - The index of `searchValue` if found, otherwise -1.
924
* @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm)
1025
*/
1126

0 commit comments

Comments
 (0)