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单线程实现多个定时器示例
Mar 30 Python
Python在Windows和在Linux下调用动态链接库的教程
Aug 18 Python
Python自动化测试ConfigParser模块读写配置文件
Aug 15 Python
Django中针对基于类的视图添加csrf_exempt实例代码
Feb 11 Python
PyQt5每天必学之拖放事件
Aug 27 Python
Django contenttypes 框架详解(小结)
Aug 13 Python
使用python生成杨辉三角形的示例代码
Aug 29 Python
python交换两个变量的值方法
Jan 12 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
Apr 09 Python
Python控制Firefox方法总结
Jun 03 Python
解决Python import docx出错DLL load failed的问题
Feb 13 Python
Python range与enumerate函数区别解析
Feb 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
《PHP边学边教》(02.Apache+PHP环境配置――上篇)
2006/12/13 PHP
PHP中exec与system用法区别分析
2014/09/22 PHP
PHP中ini_set与ini_get用法实例
2014/11/04 PHP
完美解决phpdoc导出文档中@package的warning及Error的错误
2016/05/17 PHP
利用PHP实现开心消消乐的算法示例
2017/10/12 PHP
PHP开发之用微信远程遥控服务器
2018/01/25 PHP
php 读写json文件及修改json的方法
2018/03/07 PHP
javascript 屏蔽鼠标键盘的几段代码
2008/01/02 Javascript
jquery 单击li防止重复加载的实现代码
2010/12/24 Javascript
js插件YprogressBar实现漂亮的进度条效果
2015/04/20 Javascript
浅谈jQuery页面的滚动位置scrollTop、scrollLeft
2015/05/19 Javascript
JS实现浏览器状态栏显示时间的方法
2015/10/27 Javascript
基于jQuery实现鼠标点击导航菜单水波动画效果附源码下载
2016/01/06 Javascript
Bootstrap多级菜单的实现代码
2017/05/23 Javascript
vue实现前进刷新后退不刷新效果
2018/01/26 Javascript
详解Vue.js中.native修饰符
2018/04/24 Javascript
前端js中的事件循环eventloop机制详解
2019/05/15 Javascript
[01:13:17]Secret vs NB 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
初步剖析C语言编程中的结构体
2016/01/16 Python
Python中遍历字典过程中更改元素导致异常的解决方法
2016/05/12 Python
python构建自定义回调函数详解
2017/06/20 Python
pandas 空数据处理方法详解
2019/11/02 Python
中国酒类在线零售网站:酒仙网
2016/08/20 全球购物
Gibson London官网:以地道的英国男装而著称
2019/12/06 全球购物
2014年中秋节活动总结
2014/08/29 职场文书
关于教师节的演讲稿
2014/09/04 职场文书
2014年业务工作总结
2014/11/17 职场文书
创先争优承诺书
2015/01/20 职场文书
企业催款函范本
2015/06/24 职场文书
实验室安全管理制度
2015/08/05 职场文书
小组口号霸气押韵
2015/12/24 职场文书
python生成可执行exe控制Microsip自动填写号码并拨打功能
2021/06/21 Python
深入讲解数据库中Decimal类型的使用以及实现方法
2022/02/15 MySQL
星际争霸 Light vs Action 一场把教主看到鬼畜的比赛
2022/04/01 星际争霸
redis复制有可能碰到的问题汇总
2022/04/03 Redis
Python使用BeautifulSoup4修改网页内容
2022/05/20 Python