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格式化字符串实例总结
Sep 28 Python
pymongo实现多结果进行多列排序的方法
May 16 Python
在django中使用自定义标签实现分页功能
Jul 04 Python
Python交互环境下实现输入代码
Jun 22 Python
详解Python打包分发工具setuptools
Aug 05 Python
python爬虫 基于requests模块的get请求实现详解
Aug 20 Python
Python+OpenCV+图片旋转并用原底色填充新四角的例子
Dec 12 Python
python+tifffile之tiff文件读写方式
Jan 13 Python
Python实时监控网站浏览记录实现过程详解
Jul 14 Python
python 偷懒技巧——使用 keyboard 录制键盘事件
Sep 21 Python
Python关于拓扑排序知识点讲解
Jan 04 Python
Python绘制K线图之可视化神器pyecharts的使用
Mar 02 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
10个对初学者非常有用的PHP技巧
2016/04/06 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
为原生js Array增加each方法
2012/04/07 Javascript
理解javascript中的严格模式
2016/02/01 Javascript
学习 NodeJS 第八天:Socket 通讯实例
2016/12/21 NodeJs
ES6 javascript中Class类继承用法实例详解
2017/10/30 Javascript
浅谈开发eslint规则
2018/10/01 Javascript
iview通过Dropdown(下拉菜单)实现的右键菜单
2018/10/26 Javascript
jQuery实现适用于移动端的跑马灯抽奖特效示例
2019/01/18 jQuery
解决Vue中使用keepAlive不缓存问题
2020/08/04 Javascript
探究一道价值25k的蚂蚁金服异步串行面试题
2020/08/21 Javascript
python 参数列表中的self 显式不等于冗余
2008/12/01 Python
python抓取某汽车网数据解析html存入excel示例
2013/12/04 Python
Python脚本实现网卡流量监控
2015/02/14 Python
解决python写入带有中文的字符到文件错误的问题
2019/01/31 Python
Python实现的栈、队列、文件目录遍历操作示例
2019/05/06 Python
centos+nginx+uwsgi+Django实现IP+port访问服务器
2019/11/15 Python
Python3如何对urllib和urllib2进行重构
2019/11/25 Python
flask的orm框架SQLAlchemy查询实现解析
2019/12/12 Python
10个python3常用排序算法详细说明与实例(快速排序,冒泡排序,桶排序,基数排序,堆排序,希尔排序,归并排序,计数排序)
2020/03/17 Python
Windows 下更改 jupyterlab 默认启动位置的教程详解
2020/05/18 Python
django Model层常用验证器及自定义验证器详解
2020/07/15 Python
matplotlib交互式数据光标实现(mplcursors)
2021/01/13 Python
pytorch 中forward 的用法与解释说明
2021/02/26 Python
美国知名的摄影器材销售网站:Adorama
2017/02/01 全球购物
师范教师毕业鉴定
2014/01/13 职场文书
可贵的沉默教学反思
2014/02/06 职场文书
乡镇信息公开实施方案
2014/03/23 职场文书
秋天的雨教学反思
2014/04/27 职场文书
销售个人求职信范文
2014/04/28 职场文书
环卫工作汇报材料
2014/10/28 职场文书
自我工作评价范文
2015/03/06 职场文书
2015年社区工作总结
2015/04/08 职场文书
2016年小学生寒假总结
2015/10/10 职场文书
python pyhs2 的安装操作
2021/04/07 Python
pytorch实现线性回归以及多元回归
2021/04/11 Python