Skip to content

Commit 2e3860f

Browse files
committed
Add linear search.
1 parent 7ed425e commit 2e3860f

File tree

6 files changed

+75
-14
lines changed

6 files changed

+75
-14
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ a set of rules that precisely defines a sequence of operations.
7070
* [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
7171
* [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
7272
* **Search**
73+
* [Linear Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/linear-search)
7374
* [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search)
7475
* **Sorting**
7576
* [Bubble Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bubble-sort)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Linear Search
2+
In computer science, linear search or sequential search is a
3+
method for finding a target value within a list. It sequentially
4+
checks each element of the list for the target value until a
5+
match is found or until all the elements have been searched.
6+
Linear search runs in at worst linear time and makes at most `n`
7+
comparisons, where `n` is the length of the list.
8+
9+
![Linear Search](https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif)
10+
11+
## References
12+
- [Wikipedia](https://en.wikipedia.org/wiki/Linear_search)
13+
- [TutorialsPoint](https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm)
14+
- [Youtube](https://www.youtube.com/watch?v=SGU9duLE30w)

src/algorithms/search/linear-search/Readme.md

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import linearSearch from '../linearSearch';
2+
3+
describe('linearSearch', () => {
4+
it('should search all numbers in array', () => {
5+
const array = [1, 2, 4, 6, 2];
6+
7+
expect(linearSearch(array, 10)).toEqual([]);
8+
expect(linearSearch(array, 1)).toEqual([0]);
9+
expect(linearSearch(array, 2)).toEqual([1, 4]);
10+
});
11+
12+
it('should search all strings in array', () => {
13+
const array = ['a', 'b', 'a'];
14+
15+
expect(linearSearch(array, 'c')).toEqual([]);
16+
expect(linearSearch(array, 'b')).toEqual([1]);
17+
expect(linearSearch(array, 'a')).toEqual([0, 2]);
18+
});
19+
20+
it('should search through objects as well', () => {
21+
const comparatorCallback = (a, b) => {
22+
if (a.key === b.key) {
23+
return 0;
24+
}
25+
26+
return a.key <= b.key ? -1 : 1;
27+
};
28+
29+
const array = [
30+
{ key: 5 },
31+
{ key: 6 },
32+
{ key: 7 },
33+
{ key: 6 },
34+
];
35+
36+
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual([]);
37+
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual([0]);
38+
expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual([1, 3]);
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Comparator from '../../../utils/comparator/Comparator';
2+
3+
/**
4+
* @param {*[]} array
5+
* @param {*} seekElement
6+
* @param {function(a, b)} [comparatorCallback]
7+
* @return {number[]}
8+
*/
9+
export default function linearSearch(array, seekElement, comparatorCallback) {
10+
const comparator = new Comparator(comparatorCallback);
11+
const foundIndices = [];
12+
13+
array.forEach((element, index) => {
14+
if (comparator.equal(element, seekElement)) {
15+
foundIndices.push(index);
16+
}
17+
});
18+
19+
return foundIndices;
20+
}

src/algorithms/search/linear-search/linearsearch.js

-7
This file was deleted.

0 commit comments

Comments
 (0)