用python实现将数组元素按从小到大的顺序排列方法
作者:llx1026 时间:2022-01-07 22:03:25
如下所示:
def findSmallest(arr):
smallest = arr[0]#将第一个元素的值作为最小值赋给smallest
smallest_index = 0#将第一个值的索引作为最小值的索引赋给smallest_index
for i in range(1, len(arr)):
if arr[i] < smallest:#对列表arr中的元素进行一一对比
smallest = arr[i]
smallest_index = i
return smallest_index
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)#一共要调用5次findSmallest
newArr.append(arr.pop(smallest))#每一次都把findSmallest里面的最小值删除并存放在新的数组newArr中
return newArr
print(selectionSort([5, 3, 6, 2, 10]))
运行结果如下:
[2, 3, 5, 6, 10]
来源:https://blog.csdn.net/llx1026/article/details/78024071
标签:python,数组,元素,排列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3.6 + TensorFlow 安装配置图文教程(Windows 64 bit)
2021-07-28 21:15:04
![](https://img.aspxhome.com/file/2023/1/81661_0s.png)
如何创建SQL Server 2000故障转移群集
2009-02-13 17:18:00
python数据分析之公交IC卡刷卡分析
2022-02-10 02:23:56
![](https://img.aspxhome.com/file/2023/5/75145_0s.png)
MySQL 数值类型概述int smallint tinyint
2010-11-02 11:46:00
Python jieba分词添加自定义词和去除不需要长尾词的操作方法
2023-05-05 11:20:35
![](https://img.aspxhome.com/file/2023/1/77721_0s.png)
Pytorch的安装过程之pip、conda、Docker容器安装
2021-05-20 00:07:51
asp解决fso.ReadAll提示输入超出了文件尾的错误
2008-01-30 21:40:00
iframe全跨域高度自适应解决方案
2008-12-21 16:16:00
![](https://img.aspxhome.com/file/UploadPic/200812/21/cross_domain_iframe-43s.jpg)
ASP.NET教程第二讲 ASP.NET学习
2007-08-07 12:01:00
详解Python中for循环的使用
2023-06-13 13:55:40
分享2个方便调试Python代码的实用工具
2021-08-26 18:50:08
![](https://img.aspxhome.com/file/2023/6/65856_0s.png)
python中出现invalid syntax报错的几种原因分析
2023-01-26 22:01:24
PHP之深入学习Yii2缓存Cache组件详细讲解
2023-06-29 17:37:20
![](https://img.aspxhome.com/file/2023/1/55561_0s.jpg)
用JS实现渐变效果,兼容各款浏览器
2008-09-13 19:10:00
详细解读php的命名空间(一)
2023-06-19 08:02:59
Python中的 Set 与 dict
2022-04-09 20:20:33
如何使用 Python 读取文件和照片的创建日期
2023-11-17 01:25:00
Python二次规划和线性规划使用实例
2023-08-28 05:37:10
javascript 的 in 操作符实例详解
2007-10-07 12:00:00
电子商务网站评论设计探讨
2009-12-21 16:28:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/youdao-38s.png)