# 原理
2023-04-18 16:14:05 时间
原理
定义一个同样大小数组来存方排序结果,并定义最小/最大值变量用来记录索引。
当无序集合的元素小于最小值时插入左边也就是`索引减一`的位置,
如果大于最大值则是在右边`索引加一`的位置,
其它情况按折半/直接方式插入。
原理图
暂无
实现
inputArr = [199383, 10, 34, -1,-32,-29, 4, 0, 34, 5, 4, 36, 1, 8, 123, 453, 1008]
length = len(inputArr)
sortArr = [None]*length
minIndex=maxIndex=0
sortArr[0]=inputArr[0]
print("未排序集合:{0}".format(inputArr))
for index in range(1, length):
item=inputArr[index]
# 小于最小值的放左边
if(item<sortArr[minIndex]):
minIndex=(minIndex-1+length)%length
sortArr[minIndex]=item
continue
# 大于最大值的放右边
if(item>sortArr[maxIndex]):
maxIndex=(maxIndex+1+length)%length
sortArr[maxIndex]=item
continue
# 大于最小值,小于最大值的情况需要移位插入
else:
# 将最大数向后移动1位
sortArr[(maxIndex+1+length)%length]=sortArr[maxIndex]
# 再原来的最大数位置插入待插入的值
sortArr[maxIndex]=item
# 逐个向左比较,直到找到它的正确位置
preIndex=maxIndex
while(sortArr[preIndex]<=sortArr[preIndex-1]):
sortArr[preIndex],sortArr[preIndex-1]=sortArr[preIndex-1],sortArr[preIndex]
preIndex=(preIndex-1+length)%length
maxIndex=(maxIndex+1+length)%length
print("已排序集合:{0},min:{1},max:{2}".format(sortArr,minIndex,maxIndex))
相关文章
- R语言学习—— ggplot2函数
- 【819】emerging hotspot analysis数据集
- 损失函数(lossfunction)的全面介绍(简单易懂版)
- 服务连接到redis一段时间后报错:Redis exception; nested exception is io.lettuce.core.RedisException: java.io.IOException: 你的主机中的软件中止
- 火山引擎 DataTester:一个 A/B 测试,将一款游戏的核心收益提升了 8%
- 【yolov5】将标注好的数据集进行划分(附完整可运行python代码)
- Spring Boot 分片上传、断点续传、大文件上传、秒传,应有尽有,建议收藏!!
- 浅谈kafka
- MySQL学习笔记-事务
- EntityFramworkCore7笔记 -- EFCore
- YOLOv5训练速度慢的一些解决方法
- 【Pytorch】model.train() 和 model.eval() 原理与用法
- 天机-免费的新一代数据安全网关
- 明解数据库------数据库存储演变史
- 使用PostgreSQL而不是MySQL存储中型数据有什么好处?
- 【yolov8】从0开始搭建部署YOLOv8,环境安装+推理+自定义数据集搭建与训练,一小时掌握
- 目标检测,使用最新的yolov7训练自己的数据集,从零开始的手把手教程
- 从“13 天”到“0 天”延时,揭秘火山引擎 DataLeap SLA 保障最佳实践
- Python-sqlparse解析SQL工具库一文详解(一)
- 数学建模--评价类模型