Skip to content

Commit bdc4b88

Browse files
committed
Tweak linear search
- Move author name from the index page to the actual document - You don't need to specify the `object` parameter label when calling the function
1 parent 53eb7d9 commit bdc4b88

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

Linear Search/LinearSearch.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
func linearSearch<T: Comparable>(array: [T], object:T) -> Int? {
2-
for (index, obj) in array.enumerate() where obj == object {
3-
return index
4-
}
5-
return nil
6-
}
1+
func linearSearch<T: Comparable>(array: [T], _ object: T) -> Int? {
2+
for (index, obj) in array.enumerate() where obj == object {
3+
return index
4+
}
5+
return nil
6+
}

Linear Search/README.markdown

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ We start by comparing the first number in the array, `5` with the number we're l
1515
Here is a simple implementation of insertion sort in Swift:
1616

1717
```swift
18-
func linearSearch<T: Comparable>(array: [T], object:T) -> Int? {
19-
for (index, obj) in array.enumerate() where obj == object {
20-
return index
21-
}
22-
return nil
18+
func linearSearch<T: Comparable>(array: [T], _ object: T) -> Int? {
19+
for (index, obj) in array.enumerate() where obj == object {
20+
return index
21+
}
22+
return nil
2323
}
2424
```
2525

@@ -39,3 +39,5 @@ The best-case performance is **O(1)** but this case is rare because the object w
3939
### See also
4040

4141
See also [Wikipedia](https://en.wikipedia.org/wiki/Linear_search).
42+
43+
*Written by [Patrick Balestra](http://www.github.com/BalestraPatrick)*

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If you're new to algorithms and data structures, here are a few good ones to sta
2828

2929
### Searching
3030

31-
- [Linear Search](Linear Search/) (by [Patrick Balestra](http://www.github.com/BalestraPatrick))
31+
- [Linear Search](Linear Search/)
3232
- [Binary Search](Binary Search/). Quickly find elements in a sorted array.
3333
- Count Occurrences
3434
- Select Minimum / Maximum

0 commit comments

Comments
 (0)