Python基数排序

本文阅读 1 分钟
首页 Python笔记 正文
  1. '''
  2. 实现基数排序RadixSort, 分为:
  3. 最高位优先(Most Significant Digit first)法
  4. 最低位优先(Least Significant Digit first)法
  5. '''
  6. # 最低位优先法
  7. def radixSortLSD(alist):
  8. if len(alist) == 0:
  9. return
  10. if len(alist) == 1:
  11. return alist
  12. tempList = alist
  13. maxNum = max(alist)
  14. radix = 10
  15. while maxNum * 10 > radix:
  16. newArr = [[], [], [], [], [], [], [], [], [], []]
  17. for n1 in tempList:
  18. testnum = n1 % radix
  19. testnum = testnum // (radix / 10)
  20. for n2 in range(10):
  21. if testnum == n2:
  22. newArr[n2].append(n1)
  23. tempList = []
  24. for i in range(len(newArr)):
  25. for j in range(len(newArr[i])):
  26. tempList.append(newArr[i][j])
  27. radix *= 10
  28. return tempList
  29. print(radixSortLSD([10, 12, 24, 23, 13, 52, 15, 158, 74, 32, 254, 201, 30, 19]))
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

Python快排
« 上一篇 01-21
Python一些递归算法
下一篇 » 01-21

发表评论

惪特博客
  • 文章总数:
    18497 篇
  • 评论总数:
    53318 条
  • 标签总数:
    8873 个
  • 总浏览量:
    22608542 次
  • 最后更新:
    3天前

最多点赞

随便看看

标签TAG