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中字符编码简介、方法及使用建议
Jan 08 Python
总结Python中逻辑运算符的使用
May 13 Python
Python列表推导式、字典推导式与集合推导式用法实例分析
Feb 07 Python
python使用json序列化datetime类型实例解析
Feb 11 Python
使用Python和xlwt向Excel文件中写入中文的实例
Apr 21 Python
Python中对数组集进行按行打乱shuffle的方法
Nov 08 Python
Mac下Anaconda的安装和使用教程
Nov 29 Python
Python常见数据结构之栈与队列用法示例
Jan 14 Python
scrapy-redis源码分析之发送POST请求详解
May 15 Python
Django实现文件上传下载功能
Oct 06 Python
pycharm运行scrapy过程图解
Nov 22 Python
python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例
Mar 04 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
关于时间计算的结总
2006/12/06 PHP
PHP--用万网的接口实现域名查询功能
2012/12/13 PHP
ThinkPHP模板中数组循环实例
2014/10/30 PHP
PHP指定截取字符串中的中英文或数字字符的实例分享
2016/03/18 PHP
thinkphp中AJAX返回ajaxReturn()方法分析
2016/12/06 PHP
PHP实现判断数组是一维、二维或几维的方法
2017/02/06 PHP
kindeditor 加入七牛云上传的实例讲解
2017/11/12 PHP
js限制文本框为整数和货币的函数代码
2010/10/13 Javascript
基于JQuery实现的类似购物商城的购物车
2011/12/06 Javascript
JavaScript将数据转换成整数的方法
2014/01/04 Javascript
jquery的each方法使用示例分享
2014/03/25 Javascript
Jquery跳到页面指定位置的方法
2014/05/12 Javascript
javascript中Number对象的toString()方法分析
2014/12/20 Javascript
使用Angular和Nodejs、socket.io搭建聊天室及多人聊天室
2015/08/21 NodeJs
浅谈angular懒加载的一些坑
2016/08/20 Javascript
原生Javascript和jQuery做轮播图简单例子
2016/10/11 Javascript
令按钮悬浮在(手机)页面底部的实现方法
2017/05/02 Javascript
微信小程序 五星评分的实现实例
2017/08/04 Javascript
vue使用keep-alive实现数据缓存不刷新
2017/10/21 Javascript
ng-repeat指令在迭代对象时的去重方法
2018/10/02 Javascript
js实现按钮开关单机下拉菜单效果
2018/11/22 Javascript
node.js使用fs读取文件出错的解决方案
2019/10/23 Javascript
JS简易计算器实例讲解
2020/06/30 Javascript
Vue中用JSON实现刷新界面不影响倒计时
2020/10/26 Javascript
[05:11]TI9战队采访——VIRTUSPRO
2019/08/22 DOTA
优化Python代码使其加快作用域内的查找
2015/03/30 Python
python批量制作雷达图的实现方法
2016/07/26 Python
Python使用requests发送POST请求实例代码
2018/01/25 Python
python实现贪吃蛇小游戏
2020/03/21 Python
Python基于pygame实现单机版五子棋对战
2019/12/26 Python
Python爬虫程序架构和运行流程原理解析
2020/03/09 Python
python如何爬取动态网站
2020/09/09 Python
法语专业求职信
2014/07/20 职场文书
2016年第16个全民国防教育日宣传活动总结
2016/04/05 职场文书
详解Java实践之建造者模式
2021/06/18 Java/Android
windows10声卡驱动怎么安装?win10声卡驱动安装操作步骤教程
2022/08/05 数码科技