Skip to content

Commit 85a55da

Browse files
HRIDYANSHU054Hridyanshu7appgurueu
authored
test: added for Linear Search Algorithm (TheAlgorithms#1753)
* test: added for Linear Search Algorithm * Update Search/LinearSearch.js Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> --------- Co-authored-by: Hridyanshu7 <himank7794@gmail.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent d8588f9 commit 85a55da

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Search/LinearSearch.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* value within a list. It sequentially checks each element of the list
44
* for the target value until a match is found or until all the elements
55
* have been searched.
6+
*
7+
* @see https://en.wikipedia.org/wiki/Linear_search
68
*/
79
function SearchArray(searchNum, ar, output = (v) => console.log(v)) {
810
const position = Search(ar, searchNum)

Search/test/LinearSearch.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Search as linearSearch } from '../LinearSearch'
2+
3+
const tests = [
4+
{
5+
test: {
6+
arr: [1, 2, 300, 401, 450, 504, 800, 821, 855, 900, 1002],
7+
target: 900
8+
},
9+
expectedValue: 9
10+
},
11+
{
12+
test: {
13+
arr: [1, 104, 110, 4, 44, 55, 56, 78],
14+
target: 104
15+
},
16+
expectedValue: 1
17+
},
18+
{
19+
test: {
20+
arr: [-4, 5, 50, 77, 821, 85, 99, 100],
21+
target: 192
22+
},
23+
expectedValue: -1
24+
}
25+
]
26+
27+
describe('Linear Search', () => {
28+
it.each(tests)(
29+
'linearSearch($test.arr, $test.target) => $expectedValue',
30+
({ test, expectedValue }) => {
31+
const { arr, target } = test
32+
expect(linearSearch(arr, target)).toBe(expectedValue)
33+
}
34+
)
35+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy