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 dfc54ba

Browse files
Merge pull request #8 from ms10398/master
Added Implementation of Linear Search
2 parents 2b74c16 + adbd6d7 commit dfc54ba

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎Search/linearSearch.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.
6+
*/
7+
function SearchArray(searchNum, ar) {
8+
var position = Search(ar, searchNum);
9+
if (position != -1) {
10+
console.log("The element was found at " + (position + 1));
11+
} else {
12+
console.log("The element not found");
13+
}
14+
}
15+
16+
// Search "theArray" for the specified "key" value
17+
function Search(theArray, key) {
18+
for (var n = 0; n < theArray.length; n++)
19+
if (theArray[n] == key)
20+
return n;
21+
return -1;
22+
}
23+
24+
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9];
25+
SearchArray(3, ar);
26+
SearchArray(4, ar);
27+
SearchArray(11, ar);

0 commit comments

Comments
(0)

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