Python基数排序是一种非比较整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。下面编程教程网小编给大家简单介绍一下具体代码!
Python代码实现基数排序算法
def countingSort(array, place):
size = len(array)
output = [0] * size
count = [0] * 10
for i in range(0, size):
index = array[i] // place
count[index % 10] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = size - 1
while i >= 0:
index = array[i] // place
output[count[index % 10] - 1] = array[i]
count[index % 10] -= 1
i -= 1
for i in range(0, size):
array[i] = output[i]
def radixSort(array):
# Get maximum element
max_element = max(array)
place = 1
while max_element // place > 0:
countingSort(array, place)
place *= 10
data = [121, 432, 564, 23, 1, 45, 788]
radixSort(data)
print(data)
以上是编程学习网小编为您介绍的“如何利用Python代码实现基数排序算法”的全面内容,想了解更多关于 前端知识 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:如何利用Python代码实现基数排序算法


猜你喜欢
- 23--html(css基础选择器3-id选择器) 2023-10-27
- ant design vue项目实战 2023-10-08
- vuex Getters基本用法 2023-10-08
- threejs后期处理的基本使用方法之加特效 2023-12-25
- vue项目遇到布署服务器后刷新404问题解决方案 2023-10-08
- 关于ES6中的箭头函数超详细梳理 2022-08-30
- JavaScript点击按钮或F11键盘实现全屏以及判断是否是全屏 2023-08-29
- vue3面试题:v-if 和 v-for 的优先级哪个高? 2025-01-13
- 浅谈Selenium+Webdriver 常用的元素定位方式 2024-01-05
- jQuery Ajax 实例详解 ($.ajax、$.post、$.get) 2022-10-17