Swift-桶排序

桶排序(Bucket sort)原理是将数组分到有限数量的桶里,然后
寻访序列,并且把项目一个一个放到对应的桶子去,对每个不是空的桶子进行排序,最终将所有的桶合并.
核心代码:

` func sort(arr:inout [Int],min:Int,max:Int,gap:Int) {

    var bucketlist:[[Int]] = []
    let bucketCount:Int = (max - min) / gap + 1
    
    // 建桶
    for _ in 0.. 0 {
            buketInnerSort(arr: &bucketlist[i])
        }
    }
    
    var index:Int = 0
    for i in 0.. 0 {
            for j in 0..

测试代码:

let bucketSort:BucketSort = BucketSort() var arr:[Int] = [-10, -9, -20, 29, 25, 3, 49, 9, 37, 21, 43] bucketSort.sort(arr: &arr, min: -20, max: 50, gap: 10) print("FlyElephant--桶排序---\(arr)")

你可能感兴趣的:(Swift-桶排序)