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实现的ini文件操作类分享
Nov 20 Python
Python环境下安装使用异步任务队列包Celery的基础教程
May 07 Python
Pandas探索之高性能函数eval和query解析
Oct 28 Python
Python字典,函数,全局变量代码解析
Dec 18 Python
解决Pycharm无法import自己安装的第三方module问题
May 18 Python
Sanic框架请求与响应实例分析
Jul 16 Python
Python测试模块doctest使用解析
Aug 10 Python
Django 构建模板form表单的两种方法
Jun 14 Python
Python OpenCV去除字母后面的杂线操作
Jul 05 Python
详解使用python爬取抖音app视频(appium可以操控手机)
Jan 26 Python
python字符串的多行输出的实例详解
Jun 08 Python
5道关于python基础 while循环练习题
Nov 27 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与MySQL开发的8个技巧小结
2010/12/17 PHP
一样的table?不一样的table(可编辑状态table)
2012/09/19 Javascript
javascript两种function的定义介绍及区别说明
2013/05/02 Javascript
javascript 按键事件(兼容各浏览器)
2013/12/20 Javascript
js实现文字跟随鼠标移动而移动的方法
2015/02/28 Javascript
JavaScript实现网页对象拖放功能的方法
2015/04/15 Javascript
javascript运动框架用法实例分析(实现放大与缩小效果)
2016/01/08 Javascript
DeviceOne 让你一见钟情的App快速开发平台
2016/02/17 Javascript
JavaScript事件学习小结(三)js事件对象
2016/06/09 Javascript
jQuery EasyUI中的日期控件DateBox修改方法
2016/11/09 Javascript
js数组操作方法总结(必看篇)
2016/11/22 Javascript
JS开发中百度地图+城市联动实现实时触发查询地址功能
2017/04/13 Javascript
ES6新特性四:变量的解构赋值实例
2017/04/21 Javascript
微信小程序实现瀑布流布局与无限加载的方法详解
2017/05/12 Javascript
关于vue-router的那些事儿
2018/05/23 Javascript
vue实现移动端悬浮窗效果
2018/12/01 Javascript
JS学习笔记之数组去重实现方法小结
2019/05/29 Javascript
Vue修改项目启动端口号方法
2019/11/07 Javascript
uni-app 支持多端第三方地图定位的方法
2020/01/03 Javascript
修改Python的pyxmpp2中的主循环使其提高性能
2015/04/24 Python
使用Eclipse如何开发python脚本
2018/04/11 Python
Python if语句知识点用法总结
2018/06/10 Python
用python实现名片管理系统
2020/06/18 Python
Python requests及aiohttp速度对比代码实例
2020/07/16 Python
如何利用pycharm进行代码更新比较
2020/11/04 Python
python二维图制作的实例代码
2020/12/03 Python
意大利咖啡、浓缩咖啡和浓缩咖啡机:illy caffe
2019/03/20 全球购物
好邻里事迹材料
2014/01/16 职场文书
新教师工作感言
2014/02/16 职场文书
元旦联欢会感言
2014/03/04 职场文书
怀念母亲教学反思
2014/04/28 职场文书
销售员岗位职责
2014/06/09 职场文书
报表员工作失误检讨书范文
2014/09/19 职场文书
个人总结与自我评价2015
2015/03/11 职场文书
nginx搭建图片服务器的过程详解(root和alias的区别)
2021/03/31 Servers
关于flex 上下文中自动 margin的问题(完整例子)
2021/05/20 HTML / CSS