var buckets: [[Int]] = .init(repeating:[],count:base)
the above code in book has errors message popup in xcode.
I changed to var buckets:[[Int]] = .init(repeatElement([], count: 10))
var buckets: [[Int]] = .init(repeating:[],count:base)
the above code in book has errors message popup in xcode.
I changed to var buckets:[[Int]] = .init(repeatElement([], count: 10))
What version of Xcode are you using?
Version 9.4.1 Version 9.4.1
I tested on my 9.4.1, and var buckets: [[Int]] = .init(repeating: [], count: 10)
is the correct syntax.
Itβs not clear to me why this is an error. Could you give it another try?
hi, Kelvin:
I tested again in the morning, it work fine now. it is weird.
import Foundation
extension Array where Element == Int {
public mutating func radixSort()
{
let base = 10
var done = false
var digits = 1
while !done {
//var buckets:[[Int]] = .init(repeatElement([], count: base))
var buckets: [[Int]] = .init(repeating:[],count:base)
done = true
forEach {
number in
let remainingPart = number/digits
let digit = remainingPart%base
buckets[digit].append(number)
if remainingPart > 0
{
done = false
}
}
digits *= base
self = buckets.flatMap{$0}
}
}
}