<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      算法學習:基數排序

      1、基數排序的應用場景

      把一系列單詞,按照英文字典的順序排序,如 a,alice,bob ....

      基數排序不具有普適性。

       

      2、定義

      基數排序(radix sort)屬于“分配式排序”(distribution sort),又稱“筒子法”(bucket sort)或bin sort。

      顧名思義,它是通過鍵值的部分資訊,將要排序的元素分配至某些“桶”中,以此達到排序的作用。

      基數排序法是穩定性排序其時間復雜度為O (nlog(r)m),其中r為所采取的基數,而m為堆數,在某些時候,基數排序法的效率高于其它的穩定性排序法。

       

      3、舉例

      輸入: ["banana","apple","orange","ape","he"]
      輸出: ["ape","apple","banana","he","orange"]

      4、基數排序的分類

      實現方法不一樣,但是實現的結果是一樣的。分為低位優先和高位優先。

       

      5、基數排序-低位排序-字母

      1)思路

        如果對于字母進行排序,我們的字母永遠是26個基本字母進行的組合。

        將每個字母作為一個基準的桶,只要對應位置的字母是匹配基準字母桶的,我們將其放入對應的桶里面。

        待排序列表:["banana","apple","orange","ape","he"]

        前置條件:把單詞按照最長單詞的長度進行填充(不是真正的填充,而是將對應位置看做a)

       

      2)過程說明:

        ["banana","apple","orange","ape","he"]  max_length = 6   

      取第六個單詞,idx=5:

        banana        banana[5] = a

        apple"a"      apple[5] ==>index out of range,bucket_index = 0(放入“a”)的桶中

        orange        orange[5] = e

        ape"aaa"     ape[5] ==>index out of range,bucket_index = 0(放入“a”)的桶中

        he"aaaa"     he[5]==>index out of range,bucket_index = 0(放入“a”)的桶中

      .......

      取第三個單詞,idx=2:

        banana       banana[2] = n

        apple"a"      apple[2] = p

        .......

      .......

       

      step1:分26個桶,以倒數第一個字母為基準查找

        A:banana apple ape he

        D:

        E:orange

        X:

        Y:

        Z:

      step2:從A-Z依次取值放在一起,按順序取出桶里面的數組合:banana apple ape he orange

      step3:分26個桶,按照倒數第二個字母為基準放置

        A:ape he

        D:
        E:apple
        G:orange
        ...
        N:banana
        X:
        Y:
        Z:
      step4:按順序取出桶里面輸的組合:ape he apple orange banana
      step5:分26個桶,按照(倒數)第三個字母為基準放置
        A:ape, he, banana
        G:
        L:apple
        ...
        N:orange
        X:
        Y:
        Z:
      step6:按順序取出桶里面輸的組合:ape  he banana apple orange
      ...
       

      3)代碼

      #基數排序:低位優先 字母
      def bucketSort(arr):
          #確定分桶并桶的次數
          max_length = len(max(arr,key=len))
          print(max_length)
          #while做的重復的分桶并桶
          while max_length>=0:
              buckets = [[] for i in range(26)]
              for word in arr:
                  #假定長度不夠的情況下,把單詞放到第一個桶里面去
                  if max_length>=len(word):
                      bucket_idx = 0
                  else:
                      #長度夠的情況下,取要對比的單詞word[max_length]==》獲取對應的桶的索引
                      bucket_idx = ord(word[max_length].lower())-97
                  buckets[bucket_idx].append(word)
              j = 0
              #并桶:從a - z 按順序并桶
              for bucket in buckets:
                  for word in bucket:
                      arr[j]=word
                      j+=1
              print("arr=%s"%arr)
              max_length-=1
      arr = ["banana","apple","orange","ape","he","application","bat","object","able"]
      print(bucketSort(arr))
      
      '''
      結果:
      11
      arr=['banana', 'apple', 'orange', 'ape', 'he', 'application', 'bat', 'object', 'able']
      arr=['banana', 'apple', 'orange', 'ape', 'he', 'bat', 'object', 'able', 'application']
      arr=['banana', 'apple', 'orange', 'ape', 'he', 'bat', 'object', 'able', 'application']
      arr=['banana', 'apple', 'orange', 'ape', 'he', 'bat', 'object', 'able', 'application']
      arr=['banana', 'apple', 'orange', 'ape', 'he', 'bat', 'object', 'able', 'application']
      arr=['banana', 'apple', 'orange', 'ape', 'he', 'bat', 'object', 'able', 'application']
      arr=['banana', 'apple', 'ape', 'he', 'bat', 'able', 'application', 'orange', 'object']
      arr=['ape', 'he', 'bat', 'able', 'object', 'apple', 'orange', 'application', 'banana']
      arr=['ape', 'he', 'bat', 'banana', 'able', 'object', 'apple', 'application', 'orange']
      arr=['he', 'orange', 'ape', 'object', 'able', 'banana', 'apple', 'application', 'bat']
      arr=['banana', 'bat', 'object', 'able', 'he', 'ape', 'apple', 'application', 'orange']
      arr=['able', 'ape', 'apple', 'application', 'banana', 'bat', 'he', 'object', 'orange']
      None
      '''

       

       

      6、基數排序-高位排序-字母

       1)思路

      第一步:從idx=0 開始按照字母分桶
        A:apple,ape,able====>able,ape,apple
        B:banana
        C
        D
          able,ape,apple,banana
       
      第二步:對A里面的單詞進行高位優先分桶,idx+1 = 1
        從A桶里面拿出來的:apple,ape,able
        A:
        B:able
        C
        D
        p:apple ,ape====》ape,apple
          able,ape,apple
       
      第三步:對p:apple ,ape桶里面的進行分桶,idx+1 = 2
        A
        B
        C
        D
        E: ape
        P: apple
        所有的桶只有一個單詞了,開始合桶
          ape,apple
       
      低位優先:分桶合桶
      高位優先:體現了一個分治的思想的

       

      2)代碼

      #基數排序:高位排序 字母
      def bucketSortMsd(arr,idx):
          max_length = len(max(arr,key=len))
          if idx>max_length:
              return arr
          buckets = [[] for i in range(26)]
          for word in arr:
              if idx>=len(word):
                  bucket_idx = 0
              else:
                  bucket_idx = ord(word[idx])-97
              buckets[bucket_idx].append(word)
          arr = []
          for bucket in buckets:
              if len(bucket)>1:
                  arr +=bucketSortMsd(bucket,idx+1)
              else:
                  arr+=bucket
          return arr
      arr = ["banana","apple","ape","he"]
      print(bucketSortMsd(arr,0))
      ''' 結果: ['ape', 'apple', 'banana', 'he'] '''

       

      posted @ 2020-12-08 16:13  hqq的進階日記  閱讀(528)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲高潮喷水无码AV电影| 久久综合久中文字幕青草| 亚洲岛国成人免费av| 亚洲 日本 欧洲 欧美 视频| 国产一区二区高清不卡| 亚洲人成人伊人成综合网无码| 伊人久久精品一区二区三区 | 国产欧美日韩亚洲一区二区三区| 国产成人a∨激情视频厨房| 亚洲午夜香蕉久久精品| 日本黄页网站免费大全| 尤物国产精品福利在线网| 中文人妻av高清一区二区| 最新国产AV最新国产在钱| 国产精品无遮挡又爽又黄| 中文字幕人妻中出制服诱惑| 亚洲精品日韩中文字幕| 人妻中文字幕精品一页| 成人无码视频| 国产亚洲人成网站在线观看 | 精品视频在线观看免费观看| 亚洲av乱码久久亚洲精品| 亚洲精品麻豆一二三区| 欧美丰满熟妇性xxxx| 阳泉市| 亚洲天堂av日韩精品| h动态图男女啪啪27报gif| 成人午夜在线观看日韩| 日本一区二区三区小视频| 国产在线永久视频| 午夜福利国产一区二区三区| 婷婷五月综合丁香在线| 久久综合久中文字幕青草| 超碰人人模人人爽人人喊手机版| 天天躁日日躁狠狠躁中文字幕| 99视频在线精品国自产拍| 国产AV影片麻豆精品传媒| 欧美人妻在线一区二区| 亚洲sm另类一区二区三区| 大尺度国产一区二区视频| 国产精品白浆无码流出|