Python 计数排序
2023-02-18 15:28:45 时间
Python 计数排序
Python3 实例
计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。
实例
def countSort(arr):
output = [0 for i in range(256)]
count = [0 for i in range(256)]
ans = ["" for _ in arr]
for i in arr:
count[ord(i)] += 1
for i in range(256):
count[i] += count[i-1]
for i in range(len(arr)):
output[count[ord(arr[i])]-1] = arr[i]
count[ord(arr[i])] -= 1
for i in range(len(arr)):
ans[i] = output[i]
return ans
arr = "wwwrunoobcom"
ans = countSort(arr)
print ( "字符数组排序 %s" %("".join(ans)) )
执行以上代码输出结果为:
符数组排序 bcmnoooruwww
Python3 实例
相关文章
- Biopython | 介绍和安装
- 如何正确遵守 Python 代码规范
- 如何在 python 中解决 ImportError: DLL load failed while importing win32api
- 如何在 python 中解决报错 ImportError: The 'pyparsing' package is required
- 如何使用 python 爬取酷我在线音乐
- 如何在 python 中提取图片主题色
- 数据库开发实战教程:使用Python连接Kerberos的Presto
- 赶在520之前,程序员如何用Python送上最特别的“我爱你”表白
- 【图文教程】腾讯云安装python3
- Python学习:内建属性、内建函数的教程
- [oeasy]python0030_设置路径_export_PATH_zsh_系统路径设置_export
- Python从0到1丨细说图像增强及运算
- Python图像处理丨详解图像去雾处理方法
- 盘点Python 中字符串的常用操作
- 从0到1学Python丨图像平滑方法的两种非线性滤波:中值滤波、双边滤波
- 跟我学Python图像处理丨带你入门OpenGL
- 跟我学Python图像处理丨图像分类原理与案例
- Python 中 Pickle 库的使用详解
- Python: PyQt5在Pycharm中的配置
- Python: 按位或运算符(Bitwise OR)