python选择排序算法实例总结
作者:pythoner 时间:2023-08-29 06:58:28
本文实例总结了python选择排序算法。分享给大家供大家参考。具体如下:
代码1:
def ssort(V):
#V is the list to be sorted
j = 0
#j is the "current" ordered position, starting with the first one in the list
while j != len(V):
#this is the replacing that ends when it reaches the end of the list
for i in range(j, len(V)):
#here it replaces the minor value that it finds with j position
if V[i] < V[j]:
#but it does it for every value minor than position j
V[j],V[i] = V[i],V[j]
j = j+1
#and here's the addiction that limits the verification to only the next values
return V
代码2:
def selection_sort(list):
l=list[:]
# create a copy of the list
sorted=[]
# this new list will hold the results
while len(l):
# while there are elements to sort...
lowest=l[0]
# create a variable to identify lowest
for x in l:
# and check every item in the list...
if x<lowest:
# to see if it might be lower.
lowest=x
sorted.append(lowest)
# add the lowest one to the new list
l.remove(lowest)
# and delete it from the old one
return sorted
代码3
a=input("Enter the length of the list :")
# too ask the user length of the list
l=[]
# take a emty list
for g in range (a):
# for append the values from user
b=input("Enter the element :")
# to ask the user to give list values
l.append(b)
# to append a values in a empty list l
print "The given eliments list is",l
for i in range (len(l)):
# to repeat the loop take length of l
index=i
# to store the values i in string index
num=l[i]
# to take first value in list and store in num
for j in range(i+1,len(l)):
# to find out the small value in a list read all values
if num>l[j]:
# to compare two values which store in num and list
index=j
# to store the small value of the loop j in index
num=l[j]
# to store small charecter are value in num
tem=l[i]
# to swap the list take the temparary list stor list vlaues
l[i]=l[index]
# to take first value as another
l[index]=tem
print "After the swping the list by selection sort is",l
希望本文所述对大家的Python程序设计有所帮助。
标签:python,排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python实现随机点名系统的示例代码
2023-05-05 20:53:52
![](https://img.aspxhome.com/file/2023/6/130636_0s.jpg)
go micro集成链路跟踪的方法和中间件原理解析
2024-04-26 17:29:59
![](https://img.aspxhome.com/file/2023/7/134027_0s.jpg)
用Dreamweaver MX制作文字特效
2011-06-14 09:49:47
![](https://img.aspxhome.com/file/UploadPic/20072/20072311216835s.gif)
sql2000挂起无法安装的问题的解决方法
2024-01-20 19:09:34
Python结合ImageMagick实现多张图片合并为一个pdf文件的方法
2021-01-28 20:36:55
![](https://img.aspxhome.com/file/2023/4/70744_0s.jpg)
深入理解Python爬虫代理池服务
2022-11-05 17:20:29
![](https://img.aspxhome.com/file/2023/9/100819_0s.png)
巧用weui.topTips验证数据的实例
2023-08-12 03:00:51
![](https://img.aspxhome.com/file/2023/2/55982_0s.png)
Python基础常用内建函数图文示例解析
2022-05-04 04:54:24
![](https://img.aspxhome.com/file/2023/9/93539_0s.png)
一场关于YUI3/jQuery的精彩辩论
2010-11-11 12:50:00
网站渗透常用Python小脚本查询同ip网站
2021-06-12 21:51:28
![](https://img.aspxhome.com/file/2023/1/95501_0s.png)
Vue之使用mockjs生成模拟数据案例详解
2024-05-29 22:24:40
![](https://img.aspxhome.com/file/2023/5/123105_0s.png)
Python实现暴力破解有密码的zip文件的方法
2023-11-22 22:48:40
![](https://img.aspxhome.com/file/2023/5/106975_0s.png)
解决jupyter 在浏览器中 代码不执行的问题
2023-05-08 01:21:35
![](https://img.aspxhome.com/file/2023/9/85059_0s.jpg)
Python+Pytorch实战之彩色图片识别
2022-09-12 13:57:39
![](https://img.aspxhome.com/file/2023/4/127834_0s.png)
JSON.parse处理非标准Json数据出错的解决
2024-04-18 09:49:12
Pycharm 如何设置HTML文件自动补全代码或标签
2021-08-12 17:47:15
![](https://img.aspxhome.com/file/2023/3/121473_0s.png)
python函数装饰器用法实例详解
2023-09-23 09:40:16
教你用python实现12306余票查询
2021-03-16 01:39:19
![](https://img.aspxhome.com/file/2023/7/75167_0s.png)
python数据库开发之MongoDB安装及Python3操作MongoDB数据库详细方法与实例
2024-01-22 03:53:51
![](https://img.aspxhome.com/file/2023/9/96909_0s.png)
js style动态设置table高度
2024-02-23 23:23:05