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 10 Python
在Python的Flask框架下使用sqlalchemy库的简单教程
Apr 09 Python
python中的常量和变量代码详解
Jul 25 Python
Python 200行代码实现一个滑动验证码过程详解
Jul 11 Python
pytorch多GPU并行运算的实现
Sep 27 Python
Python如何使用BeautifulSoup爬取网页信息
Nov 26 Python
Numpy之reshape()使用详解
Dec 26 Python
python语言中有算法吗
Jun 16 Python
详解python datetime模块
Aug 17 Python
利用Python pandas对Excel进行合并的方法示例
Nov 04 Python
python3 os进行嵌套操作的实例讲解
Nov 19 Python
用Python可视化新冠疫情数据
Jan 18 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 操作excel文件的方法小结
2009/12/31 PHP
完美实现GIF动画缩略图的php代码
2011/01/02 PHP
php中使用临时表查询数据的一个例子
2013/02/03 PHP
php将字符串随机分割成不同长度数组的方法
2015/06/01 PHP
为Plesk PHP7启用Oracle OCI8扩展方法总结
2019/03/29 PHP
Google韩国首页图标动画效果
2007/08/26 Javascript
iframe 上下滚动条如何默认在下方实现原理
2012/12/10 Javascript
在Node.js应用中使用Redis的方法简介
2015/06/24 Javascript
javascript实现禁止复制网页内容汇总
2015/12/30 Javascript
基于javascript实现简单计算器功能
2016/01/03 Javascript
浅析javascript函数表达式
2016/02/10 Javascript
基于Bootstrap的Metronic框架实现页面链接收藏夹功能
2016/08/29 Javascript
Bootstrap基本组件学习笔记之下拉菜单(7)
2016/12/07 Javascript
详解vue 中使用 AJAX获取数据的方法
2017/01/18 Javascript
Bootstrap警告(Alerts)的实现方法
2017/03/22 Javascript
解决VUEX刷新的时候出现数据消失
2017/07/03 Javascript
详解Vue2中组件间通信的解决全方案
2017/07/28 Javascript
利用Javascript实现一套自定义事件机制
2017/12/14 Javascript
JS基于设计模式中的单例模式(Singleton)实现封装对数据增删改查功能
2018/02/06 Javascript
详解Element 指令clickoutside源码分析
2019/02/15 Javascript
基于javascript的拖拽类封装详解
2019/04/19 Javascript
vue实现输入一位数字转汉字功能
2019/12/13 Javascript
javascript实现简单搜索功能
2020/03/26 Javascript
详解小白之KMP算法及python实现
2019/04/04 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
2019/09/10 Python
PyCharm最新激活码PyCharm2020.2.3有效
2020/11/18 Python
解决python 执行shell命令无法获取返回值的问题
2020/12/05 Python
用HTML5制作视频拼图的教程
2015/05/13 HTML / CSS
使用phonegap创建联系人的实现方法
2017/03/30 HTML / CSS
高中生期末评语大全
2014/01/28 职场文书
日语专业毕业生自荐书
2014/06/18 职场文书
学校党的群众路线教育实践活动总结报告
2014/07/03 职场文书
奥巴马当选演讲稿
2014/09/10 职场文书
毕业证委托书范文
2014/09/26 职场文书
检讨书范文大全
2015/05/07 职场文书
Python开发工具Pycharm的安装以及使用步骤总结
2021/06/24 Python