Add linear search.

This commit is contained in:
Oleksii Trekhleb 2018-05-26 01:01:08 +03:00
parent 7ed425ed3a
commit 2e3860f357
6 changed files with 75 additions and 14 deletions

View File

@ -70,6 +70,7 @@ a set of rules that precisely defines a sequence of operations.
* [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search * [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
* [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring) * [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
* **Search** * **Search**
* [Linear Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/linear-search)
* [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search) * [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search)
* **Sorting** * **Sorting**
* [Bubble Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bubble-sort) * [Bubble Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bubble-sort)

View File

@ -0,0 +1,14 @@
# Linear Search
In computer science, linear search or sequential search is a
method for finding a target value within a list. It sequentially
checks each element of the list for the target value until a
match is found or until all the elements have been searched.
Linear search runs in at worst linear time and makes at most `n`
comparisons, where `n` is the length of the list.
![Linear Search](https://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif)
## References
- [Wikipedia](https://en.wikipedia.org/wiki/Linear_search)
- [TutorialsPoint](https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm)
- [Youtube](https://www.youtube.com/watch?v=SGU9duLE30w)

View File

@ -1,7 +0,0 @@
#Linear Search
In computer science, linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.
Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list.
##References-
-[Wikipedia] https://en.wikipedia.org/wiki/Linear_search
-[Youtube] https://www.youtube.com/watch?v=SGU9duLE30w

View File

@ -0,0 +1,40 @@
import linearSearch from '../linearSearch';
describe('linearSearch', () => {
it('should search all numbers in array', () => {
const array = [1, 2, 4, 6, 2];
expect(linearSearch(array, 10)).toEqual([]);
expect(linearSearch(array, 1)).toEqual([0]);
expect(linearSearch(array, 2)).toEqual([1, 4]);
});
it('should search all strings in array', () => {
const array = ['a', 'b', 'a'];
expect(linearSearch(array, 'c')).toEqual([]);
expect(linearSearch(array, 'b')).toEqual([1]);
expect(linearSearch(array, 'a')).toEqual([0, 2]);
});
it('should search through objects as well', () => {
const comparatorCallback = (a, b) => {
if (a.key === b.key) {
return 0;
}
return a.key <= b.key ? -1 : 1;
};
const array = [
{ key: 5 },
{ key: 6 },
{ key: 7 },
{ key: 6 },
];
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual([]);
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual([0]);
expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual([1, 3]);
});
});

View File

@ -0,0 +1,20 @@
import Comparator from '../../../utils/comparator/Comparator';
/**
* @param {*[]} array
* @param {*} seekElement
* @param {function(a, b)} [comparatorCallback]
* @return {number[]}
*/
export default function linearSearch(array, seekElement, comparatorCallback) {
const comparator = new Comparator(comparatorCallback);
const foundIndices = [];
array.forEach((element, index) => {
if (comparator.equal(element, seekElement)) {
foundIndices.push(index);
}
});
return foundIndices;
}

View File

@ -1,7 +0,0 @@
function linearSearch(array, Find){
for(let i = 0; i < array.length; i++){
if(array[i] === Find) return i;
}
return -1;
}