Skip to content

Commit 353cc52

Browse files
authored
Merge pull request kodecocodes#235 from ssarber/counting-sort-swift3
Convert to Swift 3 syntax to get rid of Xcode 8 errors
2 parents c4e3f34 + 17887c3 commit 353cc52

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Counting Sort/CountingSort.playground/Contents.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//: Playground - noun: a place where people can play
22

3-
enum CountingSortError: ErrorType {
3+
enum CountingSortError: Error {
44
case ArrayEmpty
55
}
66

@@ -11,9 +11,9 @@ func countingSort(array: [Int]) throws -> [Int] {
1111

1212
// Step 1
1313
// Create an array to store the count of each element
14-
let maxElement = array.maxElement() ?? 0
14+
let maxElement = array.max() ?? 0
1515

16-
var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
16+
var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
1717
for element in array {
1818
countArray[element] += 1
1919
}
@@ -29,7 +29,7 @@ func countingSort(array: [Int]) throws -> [Int] {
2929

3030
// Step 3
3131
// Place the element in the final array as per the number of elements before it
32-
var sortedArray = [Int](count: array.count, repeatedValue: 0)
32+
var sortedArray = [Int](repeating: 0, count: array.count)
3333
for element in array {
3434
countArray[element] -= 1
3535
sortedArray[countArray[element]] = element
@@ -38,4 +38,4 @@ func countingSort(array: [Int]) throws -> [Int] {
3838
}
3939

4040

41-
try countingSort([10, 9, 8, 7, 1, 2, 7, 3])
41+
try countingSort(array: [10, 9, 8, 7, 1, 2, 7, 3])

Counting Sort/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Count 0 1 1 1 0 0 0 2 1 1 1
2020
Here is the code to accomplish this:
2121

2222
```swift
23-
let maxElement = array.maxElement() ?? 0
24-
25-
var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
23+
let maxElement = array.max() ?? 0
24+
25+
var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
2626
for element in array {
2727
countArray[element] += 1
2828
}
@@ -62,7 +62,7 @@ Output 1 2 3 7 7 8 9 10
6262
Here is the code for this final step:
6363

6464
```swift
65-
var sortedArray = [Int](count: array.count, repeatedValue: 0)
65+
var sortedArray = [Int](repeating: 0, count: array.count)
6666
for element in array {
6767
countArray[element] -= 1
6868
sortedArray[countArray[element]] = element

0 commit comments

Comments
 (0)