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日期操作学习笔记
Oct 07 Python
零基础写python爬虫之爬虫的定义及URL构成
Nov 04 Python
Python实现的简单文件传输服务器和客户端
Apr 08 Python
python实现的简单窗口倒计时界面实例
May 05 Python
Python+微信接口实现运维报警
Aug 27 Python
Python + selenium + requests实现12306全自动抢票及验证码破解加自动点击功能
Nov 23 Python
Python实现爬取马云的微博功能示例
Feb 16 Python
python 动态调用函数实例解析
Oct 21 Python
Python写捕鱼达人的游戏实现
Mar 31 Python
Django 解决上传文件时,request.FILES为空的问题
May 20 Python
python解决12306登录验证码的实现
Apr 18 Python
k-means & DBSCAN 总结
Apr 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中使用unset销毁变量并内存释放问题
2012/07/05 PHP
php中设置index.php文件为只读的方法
2013/02/06 PHP
PHP SOCKET编程详解
2015/05/22 PHP
PHP配置把错误日志以邮件方式发送方法(Windows系统)
2015/06/23 PHP
android上传图片到PHP的过程详解
2015/08/03 PHP
Ajax提交表单时验证码自动验证 php后端验证码检测
2016/07/20 PHP
select标签模拟/美化方法采用JS外挂式插件
2013/04/01 Javascript
js使用eval解析json实例与注意事项分享
2014/01/18 Javascript
node.js中的fs.statSync方法使用说明
2014/12/16 Javascript
js使用心得分享
2015/01/13 Javascript
原生Js实现简易烟花爆炸效果的方法
2015/03/20 Javascript
javascript中eval函数用法分析
2015/04/25 Javascript
18个非常棒的jQuery代码片段
2015/11/02 Javascript
Jquery删除css属性的简单方法
2016/12/04 Javascript
vue2.0构建单页应用最佳实战
2017/04/01 Javascript
详解react-redux插件入门
2018/04/19 Javascript
Vue项目中使用better-scroll实现菜单映射功能方法
2019/09/11 Javascript
Vue+elementui 实现复杂表头和动态增加列的二维表格功能
2019/09/23 Javascript
浅谈vue中$event理解和框架中在包含默认值外传参
2020/08/07 Javascript
[04:15]DOTA2-DPC中国联赛1月19日Recap集锦
2021/03/11 DOTA
Python中map和列表推导效率比较实例分析
2015/06/17 Python
详解Python如何生成词云的方法
2018/06/01 Python
Python使用LDAP做用户认证的方法
2019/06/20 Python
selenium+Chrome滑动验证码破解二(某某网站)
2019/12/17 Python
浅谈keras 的抽象后端(from keras import backend as K)
2020/06/16 Python
使用scrapy ImagesPipeline爬取图片资源的示例代码
2020/09/28 Python
python调用win32接口进行截图的示例
2020/11/11 Python
纯css3制作的火影忍者写轮眼开眼至轮回眼及进化过程实例
2014/11/11 HTML / CSS
商务英语大学生职业生涯规划书范文
2014/01/01 职场文书
竞争上岗演讲稿
2014/01/05 职场文书
产品包装策划方案
2014/05/18 职场文书
实习证明模板
2015/06/16 职场文书
小学教师读书笔记
2015/07/01 职场文书
详解nginx.conf 中 root 目录设置问题
2021/04/01 Servers
vue如何批量引入组件、注册和使用详解
2021/05/12 Vue.js
MySQL中utf8mb4排序规则示例
2021/08/02 MySQL