Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#30 from anantcodes/master
Browse files Browse the repository at this point in the history
Linear Search algorithm added
  • Loading branch information
anantcodes authored Sep 6, 2021
2 parents e9d637f + a052a26 commit 0050d1f
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Search/LinearSearch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

func linearSearch<T: Equatable>(_ array: [T], _ object: T) -> Int? {
for (index, obj) in array.enumerated() where obj == object {
return index
}
return nil
}

// The code below can be used for testing

// var numbers = [10, 119, 13, 24, 53, 17, 31, 7, 19, 627, 47, 163, 37, 611, 29, 43, 51, 41, 32]
// if let searchIndex = linearSearch(numbers,31) {
// print("Element found on index: \(searchIndex)")
// }
// else {
// print("Element not found")
// }

0 comments on commit 0050d1f

Please sign in to comment.