学习Python的第一天,实现计数排序算法;
import random def list_init(array, size): for i in range(size): array[i] = random.randint(1, size) return array def count_sort(array, size): max = array[0] min = array[0] for index in range(size): if max < array[index]: max = array[index] if min > array[index]: min = array[index] assiSize = max - min + 1 assiArray = [0] * assiSize for index in range(size): assiArray[array[index] - min] += 1 valueIndex = 0 for m in range(assiSize): n = assiArray[m] while(n > 0): array[valueIndex] = m + min n -= 1 valueIndex += 1 return array orignalList = [0] * 10 list_init(orignalList, 10) print(orignalList) count_sort(orignalList, 10) print(orignalList)
PS:Python语句末尾不带“;”,语句块不用“{}”,需要适应一下;哈哈哈
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)