mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Added Linear Search (#20)
Added algorithm for the basic and useful linear search
This commit is contained in:
parent
48195d4720
commit
7ed425ed3a
7
src/algorithms/search/linear-search/Readme.md
Normal file
7
src/algorithms/search/linear-search/Readme.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#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
|
7
src/algorithms/search/linear-search/linearsearch.js
Normal file
7
src/algorithms/search/linear-search/linearsearch.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
function linearSearch(array, Find){
|
||||||
|
for(let i = 0; i < array.length; i++){
|
||||||
|
if(array[i] === Find) return i;
|
||||||
|
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user