Group Group Group Group Group Group Group Group Group

In radix sort one code error

#1

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))

#2

What version of Xcode are you using?

#3

Version 9.4.1 Version 9.4.1

#4

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?

#5

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}
        
    }
}

}