Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f10dee

Browse files
Merge remote-tracking branch 'origin/fixing-JS-docs-for-Search-Folder' into fix-jsdocs
2 parents c70f197 + ae93e49 commit 8f10dee

11 files changed

+139
-23
lines changed

‎Search/BinarySearch.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
77
* value is found or the interval is empty.
88
*/
9-
9+
/**
10+
* Binary Search (Recursive)
11+
* @param {number[]} arr - The sorted array to search within.
12+
* @param {number} x - The value to search for in the array.
13+
* @param {number} low - The lower bound index of the search interval (default is 0).
14+
* @param {number} high - The upper bound index of the search interval (default is arr.length - 1).
15+
* @returns {number} - The index of the found element if present, otherwise -1.
16+
*/
1017
function binarySearchRecursive(arr, x, low = 0, high = arr.length - 1) {
1118
const mid = Math.floor(low + (high - low) / 2)
1219

@@ -28,6 +35,13 @@ function binarySearchRecursive(arr, x, low = 0, high = arr.length - 1) {
2835
return -1
2936
}
3037
}
38+
/**
39+
* @param {number[]} arr - The sorted array to search within.
40+
* @param {number} x - The value to search for in the array.
41+
* @param {number} low - The lower bound index of the search interval (default is 0).
42+
* @param {number} high - The upper bound index of the search interval (default is arr.length - 1).
43+
* @returns {number} - The index of the found element if present, otherwise -1.
44+
*/
3145
function binarySearchIterative(arr, x, low = 0, high = arr.length - 1) {
3246
while (high >= low) {
3347
const mid = Math.floor(low + (high - low) / 2)

‎Search/ExponentialSearch.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
*
99
*
1010
*/
11-
11+
/**
12+
* Binary Search
13+
*
14+
* @param {number[]} arr - The array to search within.
15+
* @param {number} value - The value to search for in the array.
16+
* @param {number} floor - The lower bound index of the search range.
17+
* @param {number} ceiling - The upper bound index of the search range.
18+
* @returns {number} - The index of the found element if present, otherwise -1.
19+
*/
1220
function binarySearch(arr, value, floor, ceiling) {
1321
// Middle index
1422
const mid = Math.floor((floor + ceiling) / 2)
@@ -30,7 +38,13 @@ function binarySearch(arr, value, floor, ceiling) {
3038
return binarySearch(arr, value, mid + 1, ceiling)
3139
}
3240
}
33-
41+
/**
42+
* Exponential Search
43+
* @param {number[]} arr - The array to search within.
44+
* @param {number} length - The length of the array.
45+
* @param {number} value - The value to search for in the array.
46+
* @returns {number} - The index of the found element if present, otherwise -1.
47+
*/
3448
function exponentialSearch(arr, length, value) {
3549
// If value is the first element of the array return this position
3650
if (arr[0] === value) {

‎Search/FibonacciSearch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*
1818
* We define a function fibonacciSearch() that takes an array of numbers,
1919
* the item (number) to be searched for and the length of the items in the array
20+
* @function fibonacciSearch
21+
* @param {number[]} arr - The array of numbers to search within.
22+
* @param {number} x - The number to search for in the array.
23+
* @param {number} n - The length of the array.
24+
* @returns {number} - The index of the found element if present, otherwise -1.
2025
****************************************************************************/
2126

2227
export const fibonacciSearch = (arr, x, n) => {

‎Search/InterpolationSearch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* -Worst case: O(n)
77
* -O((log(log(n))) If the data are uniformly distributed
88
*
9-
*
9+
* @param {number[]} arr - The sorted array to search in.
10+
* @param {number} key - The value to search for in the array.
11+
* @returns {number} - The index of the value in the array if found, otherwise -1
1012
*/
1113

1214
export function interpolationSearch(arr, key) {

‎Search/JumpSearch.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
/* The Jump Search algorithm allows to combine a linear search with a speed optimization.
2-
* This means that instead of going 1 by 1, we will increase the step of √n and increase that
3-
* step of √n which make the step getting bigger and bigger.
4-
* The asymptotic analysis of Jump Search is o(√n). Like the binary search, it needs to be sorted.
5-
* The advantage against binary search is that Jump Search traversed back only once.
1+
/**
2+
* The Jump Search algorithm allows combining a linear search with a speed optimization.
3+
* Instead of searching one element at a time, it increases the step size by √n each time,
4+
* making the step size progressively larger. The asymptotic analysis of Jump Search is O(√n).
5+
* Similar to binary search, the array needs to be sorted for Jump Search to work correctly.
6+
* The advantage of Jump Search over binary search is that it traverses back only once.
7+
*
8+
* @param {number[]} arr - The sorted array to search in.
9+
* @param {number} value - The value to search for in the array.
10+
* @returns {number} - The index of the value in the array if found, otherwise -1.
11+
*
12+
* @example
13+
* const arr = [1, 3, 5, 7, 9, 11, 13, 15]
14+
* const index = jumpSearch(arr, 7) // index will be 3
615
*/
7-
816
const jumpSearch = (arr, value) => {
917
const length = arr.length
1018
let step = Math.floor(Math.sqrt(length))
1119
let lowerBound = 0
20+
1221
while (arr[Math.min(step, length) - 1] < value) {
1322
lowerBound = step
1423
step += step
@@ -24,6 +33,7 @@ const jumpSearch = (arr, value) => {
2433
return -1
2534
}
2635
}
36+
2737
if (arr[lowerBound] === value) {
2838
return lowerBound
2939
}

‎Search/LinearSearch.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
/*
2-
* Linear search or sequential search is a method for finding a target
3-
* value within a list. It sequentially checks each element of the list
4-
* for the target value until a match is found or until all the elements
5-
* have been searched.
1+
/**
2+
* Linear search or sequential search is a method for finding a target value within a list.
3+
* It sequentially checks each element of the list for the target value until a match is found
4+
* or until all the elements have been searched.
5+
* @function SearchArray
6+
* @param {number} searchNum - The number to search for in the array.
7+
* @param {number[]} ar - The array in which to search for the number.
8+
* @param {(output: string) => void} [output=(v) => console.log(v)] - Optional callback function to handle output messages.
9+
* @returns {void}
10+
*
11+
* @example
12+
* // Example usage:
13+
* const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
14+
* SearchArray(3, ar) // Output: The element was found at 3
15+
* SearchArray(4, ar) // Output: The element was found at 4
16+
* SearchArray(11, ar) // Output: The element not found
617
*/
718
function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
819
const position = Search(ar, searchNum)
@@ -13,7 +24,18 @@ function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
1324
}
1425
}
1526

16-
// Search "theArray" for the specified "key" value
27+
/**
28+
* Search for a key in an array using linear search.
29+
* @function Search
30+
* @param {number[]} theArray - The array to search.
31+
* @param {number} key - The key to search for in the array.
32+
* @returns {number} - The index of the key in the array if found, otherwise -1.
33+
*
34+
* @example
35+
* const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
36+
* const index1 = Search(ar, 3) // index1 will be 2
37+
* const index2 = Search(ar, 10) // index2 will be -1
38+
*/
1739
function Search(theArray, key) {
1840
for (let n = 0; n < theArray.length; n++) {
1941
if (theArray[n] === key) {
@@ -24,8 +46,3 @@ function Search(theArray, key) {
2446
}
2547

2648
export { SearchArray, Search }
27-
28-
// const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
29-
// SearchArray(3, ar)
30-
// SearchArray(4, ar)
31-
// SearchArray(11, ar)

‎Search/QuickSelectSearch.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111
*
1212
* [Reference](http://en.wikipedia.org/wiki/Quickselect)
1313
*/
14+
/**
15+
* @function quickSelectSearch
16+
* @param {number[]} array - The array of numbers to select the `k` smallest elements from.
17+
* @param {number} k - The number of smallest elements to select.
18+
* @returns {number[]} - A slice of the `k` smallest elements from the array.
19+
* @throws {Error} - Throws an error if the array is empty or if `k` is greater than or equal to the array length.
20+
* @example
21+
* const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110]
22+
* const result1 = quickSelectSearch(arr, 5) // [19, 21, 28, 41, 5]
23+
* const result2 = quickSelectSearch(arr, 2) // [19, 5]
24+
* const result3 = quickSelectSearch(arr, 7) // [19, 5, 21, 41, 28, 66, 333]
25+
*
26+
* @see {@link http://en.wikipedia.org/wiki/Quickselect}
27+
*/
1428
export function quickSelectSearch(array, k) {
1529
if (!array || array.length <= k) {
1630
throw new Error('Invalid arguments')

‎Search/RabinKarp.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
*
1313
* [Reference](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm)
1414
*/
15+
/**
16+
* @param {string} text - The text string in which to search for the pattern.
17+
* @param {string} pattern - The pattern string to search for in the text.
18+
* @returns {number[]} - An array of indices where the pattern is found in the text. Returns an empty array if the pattern is not found.
19+
*
20+
* @example
21+
* // Example usage:
22+
* const text = 'abracadabra'
23+
* const pattern = 'cad'
24+
* const indices = rabinKarpSearch(text, pattern)
25+
* console.log(indices) // Output: [3]
26+
*
27+
* @see {@link https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm}
28+
*/
1529

1630
const BASE = 256 // The number of characters in the alphabet
1731
const MOD = 997 // A prime number used for the hash function

‎Search/StringSearch.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/*
22
* String Search
33
*/
4-
4+
/**
5+
* Builds a prefix table for String where table[i] store prefix of lengest prefix of
6+
* substring str[0..i]
7+
* @param {string} str - The word to build the prefix table for.
8+
* @returns {number[]} - The prefix table for the word.
9+
*/
510
function makeTable(str) {
611
// create a table of size equal to the length of `str`
712
// table[i] will store the prefix of the longest prefix of the substring str[0..i]
@@ -35,6 +40,11 @@ function makeTable(str) {
3540
}
3641

3742
// Find all the words that matches in a given string `str`
43+
/**
44+
* @param {string} str - The main text string to search within.
45+
* @param {string} word - The word to search for within the text.
46+
* @returns {number[]} - An array of indices where the word matches occur in the text.
47+
*/
3848
export function stringSearch(str, word) {
3949
// find the prefix table in O(n)
4050
const prefixes = makeTable(word)

‎Search/TernarySearch.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
*
1111
* Reference: https://www.geeksforgeeks.org/ternary-search/
1212
*/
13+
/**
14+
*
15+
* @param {number[]} arr - The sorted array to search in.
16+
* @param {number} key - The key to search for.
17+
* @param {number} [low=0] - The lowest index of the search range.
18+
* @param {number} [high=arr.length - 1] - The highest index of the search range.
19+
* @returns {number} - The index of the key if found, otherwise -1.
20+
*/
1321

1422
function ternarySearchRecursive(arr, key, low = 0, high = arr.length - 1) {
1523
if (high >= low) {
@@ -46,7 +54,13 @@ function ternarySearchRecursive(arr, key, low = 0, high = arr.length - 1) {
4654
return -1
4755
}
4856
}
49-
57+
/**
58+
* @param {number[]} arr - The sorted array to search in.
59+
* @param {number} key - The key to search for.
60+
* @param {number} [low=0] - The lowest index of the search range.
61+
* @param {number} [high=arr.length - 1] - The highest index of the search range.
62+
* @returns {number} - The index of the key if found, otherwise -1.
63+
*/
5064
function ternarySearchIterative(arr, key, low = 0, high = arr.length - 1) {
5165
while (high >= low) {
5266
// find the mid1 and mid2

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /