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 20 Python
python通过getopt模块如何获取执行的命令参数详解
Dec 29 Python
Python 如何提高元组的可读性
Aug 26 Python
用python画一只可爱的皮卡丘实例
Nov 21 Python
pytorch中torch.max和Tensor.view函数用法详解
Jan 03 Python
python3利用Axes3D库画3D模型图
Mar 25 Python
python matplotlib.pyplot.plot()参数用法
Apr 14 Python
Python+PyQt5+MySQL实现天气管理系统
Jun 16 Python
PyCharm中配置PySide2的图文教程
Jun 18 Python
PyCharm 2020.2.2 x64 下载并安装的详细教程
Oct 15 Python
Python爬虫基础之爬虫的分类知识总结
May 13 Python
Python-OpenCV教程之图像的位运算详解
Jun 21 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
杏林同学录(九)
2006/10/09 PHP
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
2014/01/03 PHP
Yii核心验证器api详解
2016/11/23 PHP
Javascript----文件操作
2007/01/18 Javascript
JavaScript 在网页上单击鼠标的地方显示层及关闭层
2012/12/30 Javascript
js操作模态窗口及父子窗口间相互传值示例
2014/06/09 Javascript
js微信分享API
2020/10/11 Javascript
json定义及jquery操作json的方法
2016/10/03 Javascript
分分钟玩转Vue.js组件
2016/10/25 Javascript
Node.js创建Web、TCP服务器
2017/12/05 Javascript
老生常谈JS中的继承及实现代码
2018/07/06 Javascript
jQuery实现动态生成年月日级联下拉列表示例
2019/05/11 jQuery
layui点击左侧导航栏,实现不刷新整个页面,只刷新局部的方法
2019/09/25 Javascript
JS实现水平移动与垂直移动动画
2019/12/19 Javascript
Python实现压缩和解压缩ZIP文件的方法分析
2017/09/28 Python
教你使用python实现微信每天给女朋友说晚安
2018/03/23 Python
在NumPy中创建空数组/矩阵的方法
2018/06/15 Python
python如何创建TCP服务端和客户端
2018/08/26 Python
解决django前后端分离csrf验证的问题
2019/02/03 Python
pycharm配置git(图文教程)
2019/08/16 Python
给你一面国旗 教你用python画中国国旗
2019/09/24 Python
python爬虫爬取笔趣网小说网站过程图解
2019/11/18 Python
Pytorch之view及view_as使用详解
2019/12/31 Python
Python实现汇率转换操作
2020/05/03 Python
简单了解Django项目应用创建过程
2020/07/06 Python
美国时尚女装在线:Missguided
2016/12/03 全球购物
英国现代市场:ARKET
2019/04/10 全球购物
七年级历史教学反思
2014/02/05 职场文书
党校学习自我鉴定
2014/02/24 职场文书
优秀教师演讲稿
2014/05/06 职场文书
个人自查自纠材料
2014/10/14 职场文书
整脏治乱工作简报
2015/07/21 职场文书
大学毕业生自我鉴定范文
2019/06/21 职场文书
如何理解Vue简单状态管理之store模式
2021/05/15 Vue.js
只需要这一行代码就能让python计算速度提高十倍
2021/05/24 Python
一篇文章弄懂Python中的内建函数
2021/08/07 Python