2024年03月27日 建站教程
Python基数排序是一种非比较整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。下面web建站小编给大家简单介绍一下具体代码!
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)
本文链接:http://so.lmcjl.com/news/491/