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导出数据到Excel可读取的CSV文件的方法
May 12 Python
Python 实现网页自动截图的示例讲解
May 17 Python
python操作mysql代码总结
Jun 01 Python
浅谈Python traceback的优雅处理
Aug 31 Python
Python os.rename() 重命名目录和文件的示例
Oct 25 Python
Python numpy中矩阵的基本用法汇总
Feb 12 Python
Python实现获取系统临时目录及临时文件的方法示例
Jun 26 Python
django框架模型层功能、组成与用法分析
Jul 30 Python
Django获取应用下的所有models的例子
Aug 30 Python
Python 正则表达式爬虫使用案例解析
Sep 23 Python
基于jupyter代码无法在pycharm中运行的解决方法
Apr 21 Python
Matlab中plot基本用法的具体使用
Jul 17 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
PHP 和 XML: 使用expat函数(二)
2006/10/09 PHP
PHP采集相关教程之一 CURL函数库
2010/02/15 PHP
解析dedecms空间迁移步骤详解
2013/05/15 PHP
PHP常用字符串输出方法分析(echo,print,printf及sprintf)
2021/03/09 PHP
javascript 图片上一张下一张链接效果代码
2010/03/12 Javascript
HTML5附件拖拽上传drop &amp; google.gears实现代码
2011/04/28 Javascript
jquery禁止输入数字以外的字符的示例(纯数字验证码)
2014/04/10 Javascript
JS实现判断滚动条滚到页面底部并执行事件的方法
2014/12/18 Javascript
jQuery浏览器CSS3特写兼容实例
2015/01/19 Javascript
JavaScript取得键盘按下方向键是哪个的方法
2015/08/04 Javascript
Three.js学习之文字形状及自定义形状
2016/08/01 Javascript
javascript比较语义化版本号的实现代码
2016/09/09 Javascript
canvas学习之API整理笔记(一)
2016/12/29 Javascript
Vue一个案例引发的递归组件的使用详解
2018/11/15 Javascript
解决Layui中layer报错的问题
2019/09/03 Javascript
JavaScript 监听组合按键思路及代码实现
2020/07/28 Javascript
[01:52]2020年DOTA2 TI10夏季活动预告片
2020/07/15 DOTA
[02:50]【扭转乾坤,只此一招】DOTA2永雾林渊版本开启新篇章
2020/12/22 DOTA
python使用正则表达式替换匹配成功的组并输出替换的次数
2017/11/22 Python
python2.7到3.x迁移指南
2018/02/01 Python
python快速建立超简单的web服务器的实现方法
2018/02/17 Python
Python OpenCV读取显示视频的方法示例
2020/02/20 Python
python3 使用openpyxl将mysql数据写入xlsx的操作
2020/05/15 Python
CAT鞋英国官网:坚固耐用的靴子和鞋
2016/10/21 全球购物
俄罗斯旅游网站:Tripadvisor俄罗斯
2017/03/21 全球购物
英国剑桥包中文官网:The Cambridge Satchel Company中国
2018/11/06 全球购物
美国孕妇装购物网站:Motherhood Maternity
2019/09/22 全球购物
欧舒丹俄罗斯官方网站:L’OCCITANE俄罗斯
2019/11/22 全球购物
工商管理实习自我鉴定
2013/09/28 职场文书
函授毕业自我鉴定
2014/02/04 职场文书
社区党建工作方案
2014/06/10 职场文书
教师党员个人整改措施材料
2014/09/16 职场文书
2016个人先进事迹材料范文
2016/03/01 职场文书
python实现腾讯滑块验证码识别
2021/04/27 Python
Js类的构建与继承案例详解
2021/09/15 Javascript
搭建Yolov5服务器
2022/04/30 Servers