Skip to content

Commit

Permalink
Merge pull request #235 from ssarber/counting-sort-swift3
Browse files Browse the repository at this point in the history
Convert to Swift 3 syntax to get rid of Xcode 8 errors
  • Loading branch information
kelvinlauKL committed Sep 27, 2016
2 parents c4e3f34 + 17887c3 commit 353cc52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Counting Sort/CountingSort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//: Playground - noun: a place where people can play

enum CountingSortError: ErrorType {
enum CountingSortError: Error {
case ArrayEmpty
}

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

// Step 1
// Create an array to store the count of each element
let maxElement = array.maxElement() ?? 0
let maxElement = array.max() ?? 0

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

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


try countingSort([10, 9, 8, 7, 1, 2, 7, 3])
try countingSort(array: [10, 9, 8, 7, 1, 2, 7, 3])
8 changes: 4 additions & 4 deletions Counting Sort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Count 0 1 1 1 0 0 0 2 1 1 1
Here is the code to accomplish this:

```swift
let maxElement = array.maxElement() ?? 0
var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
let maxElement = array.max() ?? 0

var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
for element in array {
countArray[element] += 1
}
Expand Down Expand Up @@ -62,7 +62,7 @@ Output 1 2 3 7 7 8 9 10
Here is the code for this final step:

```swift
var sortedArray = [Int](count: array.count, repeatedValue: 0)
var sortedArray = [Int](repeating: 0, count: array.count)
for element in array {
countArray[element] -= 1
sortedArray[countArray[element]] = element
Expand Down

0 comments on commit 353cc52

Please sign in to comment.