python选择排序算法实例总结


Posted in Python onJuly 01, 2015

本文实例总结了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 相关文章推荐
python正则匹配查询港澳通行证办理进度示例分享
Dec 27 Python
高性能web服务器框架Tornado简单实现restful接口及开发实例
Jul 16 Python
Python实现的一个简单LRU cache
Sep 26 Python
python提取页面内url列表的方法
May 25 Python
详解C++编程中一元运算符的重载
Jan 19 Python
使用Python的Django和layim实现即时通讯的方法
May 25 Python
Python数据类型之Number数字操作实例详解
May 08 Python
Pytorch Tensor的索引与切片例子
Aug 18 Python
Python numpy线性代数用法实例解析
Nov 15 Python
Python可变参数会自动填充前面的默认同名参数实例
Nov 18 Python
解决windows下python3使用multiprocessing.Pool出现的问题
Apr 08 Python
python中有函数重载吗
May 28 Python
python实现的希尔排序算法实例
Jul 01 #Python
python获取一组汉字拼音首字母的方法
Jul 01 #Python
python的keyword模块用法实例分析
Jun 30 #Python
Python实现监控程序执行时间并将其写入日志的方法
Jun 30 #Python
python实现爬取千万淘宝商品的方法
Jun 30 #Python
python简单判断序列是否为空的方法
Jun 30 #Python
python检查序列seq是否含有aset中项的方法
Jun 30 #Python
You might like
WordPress中用于获取搜索表单的PHP函数使用解析
2016/01/05 PHP
php readfile()修改文件上传大小设置
2017/08/11 PHP
firefox中用javascript实现鼠标位置的定位
2007/06/17 Javascript
js隐式全局变量造成的bug示例代码
2014/04/22 Javascript
JQuery 给元素绑定click事件多次执行的解决方法
2014/09/09 Javascript
DOM基础教程之使用DOM + Css
2015/01/20 Javascript
Jquery中find与each方法用法实例
2015/02/04 Javascript
JS实现当前页居中分页效果的方法
2015/06/18 Javascript
javascript自动恢复文本框点击清除后的默认文本
2016/01/12 Javascript
ajax跨域调用webservice的实现代码
2016/05/09 Javascript
JS中Select下拉列表类(支持输入模糊查询)功能
2017/01/17 Javascript
利用Jquery实现几款漂亮实用的时间轴(附示例代码)
2017/02/15 Javascript
详解node.js搭建代理服务器请求数据
2017/04/08 Javascript
Angular排序实例详解
2017/06/28 Javascript
React进阶学习之组件的解耦之道
2017/08/07 Javascript
Three.js利用性能插件stats实现性能监听的方法
2017/09/25 Javascript
详解mpvue开发小程序小总结
2018/07/25 Javascript
微信小程序实现一个简单swiper代码实例
2019/12/30 Javascript
Vue props中Object和Array设置默认值操作
2020/07/30 Javascript
openlayers实现图标拖动获取坐标
2020/09/25 Javascript
[00:57]英雄,你的补给到了!
2020/11/13 DOTA
python隐藏终端执行cmd命令的方法
2019/06/24 Python
用Python抢火车票的简单小程序实现解析
2019/08/14 Python
python实现的生成word文档功能示例
2019/08/23 Python
浅谈Python类中的self到底是干啥的
2019/11/11 Python
Python request post上传文件常见要点
2020/11/20 Python
Web时代变迁及html5与html4的区别
2016/01/06 HTML / CSS
美国南加州的原创极限运动潮牌:Vans(范斯)
2016/08/05 全球购物
PHP面试题-$message和$$message的区别
2015/12/08 面试题
STP协议的主要用途是什么?为什么要用STP
2012/12/20 面试题
企业元宵节主持词
2014/03/25 职场文书
《故乡》教学反思
2014/04/10 职场文书
技术股东合作协议书
2014/12/02 职场文书
数学备课组工作总结
2015/08/12 职场文书
浅谈:电影《孔子》观后感(范文)
2019/10/14 职场文书
python Django框架快速入门教程(后台管理)
2021/07/21 Python