8000 Merge pull request #30 from anantcodes/master · delphi1977/Swift@0050d1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0050d1f

Browse files
authored
Merge pull request TheAlgorithms#30 from anantcodes/master
Linear Search algorithm added
2 parents e9d637f + a052a26 commit 0050d1f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Search/LinearSearch.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Foundation
2+
3+
func linearSearch<T: Equatable>(_ array: [T], _ object: T) -> Int? {
4+
for (index, obj) in array.enumerated() where obj == object {
5+
return index
6+
}
7+
return nil
8+
}
9+
10+
// The code below can be used for testing
11+
12+
// var numbers = [10, 119, 13, 24, 53, 17, 31, 7, 19, 627, 47, 163, 37, 611, 29, 43, 51, 41, 32]
13+
// if let searchIndex = linearSearch(numbers,31) {
14+
// print("Element found on index: \(searchIndex)")
15+
// }
16+
// else {
17+
// print("Element not found")
18+
// }

0 commit comments

Comments
 (0)
0