zoukankan      html  css  js  c++  java
  • Algorithms

    概念
        基数排序 radix sort 是先按最低有效位进行排序来解决排序问题. 
        下图是一个7个3位数的继续排序徐排序. '排序基数' 的顺序依次为: 个位 -> 十位 -> 百位
        对每一个排序基数进行排序的时候需要保证排序的 '稳定性'. 
    即: 具有相同的元素值在输出数组中的相对次序与它们在输入数组中的相对次序相同.
    也就是说, 输入中的两个相同的数来说, 先出现的数也会先出现在输出数组中。
           
          
    Python programming
    
    import counting_sort    #  基数排序是一种稳定的排序算法  https://www.cnblogs.com/zzyzz/p/12889386.html
    
    def radix_sort(A, d):
        n = len(A)
        B = [0 for z in range(n)]
        for i in range(d):
            # use a stable sort to sort arrary A on digit i
            # 处理排序基数 : 个位 -> 十位 -> 百位
            x = []
            res = ['' for y in range(n)]
            for item in A:
                x.append(int(str(item)[3-i-1]))
            print('Factor :', i+1, x)
    
            # 通过计算排序法对排序因子进行稳定排序
            temp = counting_sort.counting_sort(x,[0 for x in range(7)],max(x)+1)
            print('Sorted Factor :', i+1, temp)
            k = 0
    
            #根据排序基数的排序结果. 对数组进行排序
            while k < n:
                cur = x.count(temp[k])
                print('index :', k, cur)
                ind = 0
                iter = cur
                while iter:
                    # find the correct ind in the A
                    count = 0
                    for i in range(n):
                        if x[i] == temp[k]:
                            count += 1
                        if count == ind+1:
                            B[k + ind] = A[i]
                            break
                    ind += 1
                    iter -= 1
                k += cur
            print('Temp :',i, B)
    
            # updated A,   在下一次循环之前需要更新数组 A.
            import copy
            A = copy.deepcopy(B)
            print('Sorted:',i, A)
    
    
    if __name__ == '__main__':
        A = [329, 457, 657, 839, 436, 720, 355]
        radix_sort(A, 3)
    
    结果打印:
    Factor : 1 [9, 7, 7, 9, 6, 0, 5]                # 排序基数:  个位
    111 [1, 0, 0, 0, 0, 1, 1, 2, 0, 2]             # counting sort 过程
    222 [1, 1, 1, 1, 1, 2, 3, 5, 5, 7]
    Steps :  6
    Indexs :  5 1
    B [0, 5, 0, 0, 0, 0, 0]
    C [1, 1, 1, 1, 1, 1, 3, 5, 5, 7]
    Steps :  5
    Indexs :  0 0
    B [0, 5, 0, 0, 0, 0, 0]
    C [0, 1, 1, 1, 1, 1, 3, 5, 5, 7]
    Steps :  4
    Indexs :  6 2
    B [0, 5, 6, 0, 0, 0, 0]
    C [0, 1, 1, 1, 1, 1, 2, 5, 5, 7]
    Steps :  3
    Indexs :  9 6
    B [0, 5, 6, 0, 0, 0, 9]
    C [0, 1, 1, 1, 1, 1, 2, 5, 5, 6]
    Steps :  2
    Indexs :  7 4
    B [0, 5, 6, 0, 7, 0, 9]
    C [0, 1, 1, 1, 1, 1, 2, 4, 5, 6]
    Steps :  1
    Indexs :  7 3
    B [0, 5, 6, 7, 7, 0, 9]
    C [0, 1, 1, 1, 1, 1, 2, 3, 5, 6]
    Steps :  0
    Indexs :  9 5
    B [0, 5, 6, 7, 7, 9, 9]
    C [0, 1, 1, 1, 1, 1, 2, 3, 5, 5]
    Sorted Factor : 1 [0, 5, 6, 7, 7, 9, 9]       # 排序基数:  个位排序结果
    index : 0 1                                            # 根据排序counting sort的排序结果. 对数组进行排序
    index : 1 1
    index : 2 1
    index : 3 2
    index : 5 2
    Temp : 3 [720, 355, 436, 457, 657, 329, 839]   
    Sorted: 3 [720, 355, 436, 457, 657, 329, 839]      # 排序基数:  个位排序后数组临时结果
    Factor : 2 [2, 5, 3, 5, 5, 2, 3]                 # 排序基数:  十位排序结果
    111 [0, 0, 2, 2, 0, 3]                             #  计数排序 counting sort 的过程
    222 [0, 0, 2, 4, 4, 7]
    Steps :  6
    Indexs :  3 3
    B [0, 0, 0, 3, 0, 0, 0]
    C [0, 0, 2, 3, 4, 7]
    Steps :  5
    Indexs :  2 1
    B [0, 2, 0, 3, 0, 0, 0]
    C [0, 0, 1, 3, 4, 7]
    Steps :  4
    Indexs :  5 6
    B [0, 2, 0, 3, 0, 0, 5]
    C [0, 0, 1, 3, 4, 6]
    Steps :  3
    Indexs :  5 5
    B [0, 2, 0, 3, 0, 5, 5]
    C [0, 0, 1, 3, 4, 5]
    Steps :  2
    Indexs :  3 2
    B [0, 2, 3, 3, 0, 5, 5]
    C [0, 0, 1, 2, 4, 5]
    Steps :  1
    Indexs :  5 4
    B [0, 2, 3, 3, 5, 5, 5]
    C [0, 0, 1, 2, 4, 4]
    Steps :  0
    Indexs :  2 0
    B [2, 2, 3, 3, 5, 5, 5]
    C [0, 0, 0, 2, 4, 4]
    Sorted Factor : 2 [2, 2, 3, 3, 5, 5, 5]             # 排序基数:  十位排序结果
    index : 0 2                                                  # 根据排序counting sort的排序结果. 对数组进行排序
    index : 2 2
    index : 4 3
    Temp : 4 [720, 329, 436, 839, 355, 457, 657]
    Sorted: 4 [720, 329, 436, 839, 355, 457, 657]        # 排序基数:  十位排序后数组临时结果
    Factor : 3 [7, 3, 4, 8, 3, 4, 6]                                # 排序基数:  百位排序结果
    111 [0, 0, 0, 2, 2, 0, 1, 1, 1]                                 # counting sort 排序过程
    222 [0, 0, 0, 2, 4, 4, 5, 6, 7]
    Steps :  6
    Indexs :  6 4
    B [0, 0, 0, 0, 6, 0, 0]
    C [0, 0, 0, 2, 4, 4, 4, 6, 7]
    Steps :  5
    Indexs :  4 3
    B [0, 0, 0, 4, 6, 0, 0]
    C [0, 0, 0, 2, 3, 4, 4, 6, 7]
    Steps :  4
    Indexs :  3 1
    B [0, 3, 0, 4, 6, 0, 0]
    C [0, 0, 0, 1, 3, 4, 4, 6, 7]
    Steps :  3
    Indexs :  8 6
    B [0, 3, 0, 4, 6, 0, 8]
    C [0, 0, 0, 1, 3, 4, 4, 6, 6]
    Steps :  2
    Indexs :  4 2
    B [0, 3, 4, 4, 6, 0, 8]
    C [0, 0, 0, 1, 2, 4, 4, 6, 6]
    Steps :  1
    Indexs :  3 0
    B [3, 3, 4, 4, 6, 0, 8]
    C [0, 0, 0, 0, 2, 4, 4, 6, 6]
    Steps :  0
    Indexs :  7 5
    B [3, 3, 4, 4, 6, 7, 8]
    C [0, 0, 0, 0, 2, 4, 4, 5, 6]
    Sorted Factor : 3 [3, 3, 4, 4, 6, 7, 8]                 # 排序基数:  百位排序结果
    index : 0 2                                                      # 根据排序counting sort的排序结果. 对数组进行排序
    index : 2 2
    index : 4 1
    index : 5 1
    index : 6 1
    Temp : 3 [329, 355, 436, 457, 657, 720, 839]   
    Sorted: 3 [329, 355, 436, 457, 657, 720, 839]  # 排序基数:  百位排序后数组临时结果. 由于数组A的元素是由3位数构成的, 所以百位基数排序后的结果即为最终结果

    Reference,

      1. Introduction to algorithms

  • 相关阅读:
    flex 内嵌js文件
    LOADRUNNER 录制脚本的问题
    loadrunner windows 2003 添加性能计数器的问题
    其他的网站能上,微软网站不能上是由病毒引起的
    sql express 静默安装的方方法
    javascript 附件事件时带参数的处理方法
    java 面试题 转载
    windows 域共享的问题
    winform listbox 没有listitem的问题
    对象相交检测示例
  • 原文地址:https://www.cnblogs.com/zzyzz/p/12905865.html
Copyright © 2011-2022 走看看