Sorting Algorithms swift :( IX) radix sort

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/lin1109221208/article/details/90694120

1 Overview

Radix sort belong to assign ordering, also known as bucket method, he was part of the consultation through the key will be assigned to some sort of element in the bucket, in order to achieve the sort of role.

Radix sort is part of a stable sort of

2, algorithm principle

Rationale: The use of barrels to achieve, and then follow the base of the barrel, the base values ​​from low to high numbers of this value

step:

1) random number sequence as the base value, the value of the random sequence of base into the corresponding bucket

After 2) th digit as the base into the tub is completed, the data in the general installation number in ascending order of the tub taken, then stored in array before

3) On the basis of the array 2 and then generated on the basis of the number of bits to the formula into the tub, into the tub after completion of the sequential order values ​​taken out of the tub again

NOTE: The larger the value of the sort, the more the number of the tub into the tub, so as the number of bits increases, efficiency decreases

3, for example

In [62, 88, 58, 47, 62, 35, 73, 51, 99, 37, 93] Sorting Example

1) taken as the base number, to 62, for example, taken as the base number, in the round 62 to enter the number of the tub 2

2) 1) After sorting, the data in descending removed bucket mounting numbers stored before the array

3) Take the tens digit of the base into the tub, remove the sort order of finish, the most significant bit in the array is ten digits, i.e., the ten digits after the tub into the tub, the array is already sorted completed, if there is high, the need to continue taking cardinality sorting, sorting is done up to the highest position

 

4, algorithm

//1、创建n个空桶
    /*
     返回结果的类型是Array<Array<Int>>,是一个二维数组。
     内层数组就是一个桶,负责存放与该桶编号相等的基数对应的数值
     */
    private func createBucket(_ n : Int)->Array<Array<Int>>{
        var bucket : Array<Array<Int>> = []
        for _ in 0..<n {
            bucket.append([])
        }
        return bucket
    }
    //2、计算无序序列中最大的那个数
    /*
     取基数入桶出桶的次数以此最大数值的位数为准
     */
    private func listMaxItem(_ list : Array<Int>)->Int{
        var maxNumber = list[0]
        for item in list {
            if maxNumber < item {
                maxNumber = item
            }
        }
        return maxNumber
    }
    //3、获取数字的长度----即取基数的次数
    private func numberLength(_ num : Int)->Int{
        return "\(num)".count
    }
    //4、获取数值中特定位数的值
    /*
     通过取余以及求模的方式获取 / 采用将数字转换成字符串,然后将字符串转换成字符串数组
     */
    private class func fetchBaseNumber(_ num : Int, _ digit : Int)->Int{
        if digit > 0 && digit<=SortSummary.numberLength(num) {
            var numArr : Array<Int> = []
            for char in "\(num)" {
                numArr.append(Int("\(char)")!)
            }
            return numArr[numArr.count-digit]
        }
        return 0
    }
    //5、排序
    func radixSort(_ list : inout Array<Int>){
        var bucket = createBucket(list.count)
        var maxNum = listMaxItem(list)
        let maxLength = numberLength(maxNum)
        
        for digit in 1...maxLength {
            //入桶
            for item in list {
                let baseNum = fetchBaseNumber(item, digit)
                //根据基数进入相应的桶中
                bucket[baseNum].append(item)
            }
            //出桶
            var index = 0
            for i in 0..<bucket.count{
                while !bucket[i].isEmpty {
                    list[index] = bucket[i].remove(at: 0)
                    index += 1
                }
            }
        }
    }



调用:
        var array9 = [62, 88, 58, 47, 62, 35, 73, 51, 99, 37, 93]
        radixSort(&array9)


运行结果:
[35, 37, 47, 51, 58, 62, 62, 73, 88, 93, 99] 

5, the time complexity

Radix sorting time complexity is O (d (n + radix)), wherein the dispensing trip time complexity is O (n), collecting trip time complexity is O (radix), a total distribution and collection times d

 

github Code

Note: The sort of specific implementation code in  SortSummary.swift  calling file is in ViewController.swift

Guess you like

Origin blog.csdn.net/lin1109221208/article/details/90694120