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爬虫之HTTP异常处理
Nov 05 Python
python中list常用操作实例详解
Jun 03 Python
不知道这5种下划线的含义,你就不算真的会Python!
Oct 09 Python
Pycharm无法使用已经安装Selenium的解决方法
Oct 13 Python
pycharm运行出现ImportError:No module named的解决方法
Oct 13 Python
Python 数值区间处理_对interval 库的快速入门详解
Nov 16 Python
python 表格打印代码实例解析
Oct 12 Python
pytorch中的自定义反向传播,求导实例
Jan 06 Python
python opencv如何实现图片绘制
Jan 19 Python
python3使用Pillow、tesseract-ocr与pytesseract模块的图片识别的方法
Feb 26 Python
Django如何在不停机的情况下创建索引
Aug 02 Python
编写python程序的90条建议
Apr 14 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
全国FM电台频率大全 - 6 辽宁省
2020/03/11 无线电
PHP eval函数使用介绍
2013/12/08 PHP
JQuery 获取和设置Select选项的代码
2010/02/07 Javascript
锋利的jQuery 第三章章节总结的例子
2010/03/23 Javascript
基于JavaScript实现 获取鼠标点击位置坐标的方法
2013/04/12 Javascript
JS验证日期的格式YYYY-mm-dd 具体实现
2013/06/29 Javascript
浅析javascript中函数声明和函数表达式的区别
2015/02/15 Javascript
JavaScript动态创建div等元素实例讲解
2016/01/06 Javascript
Knockout结合Bootstrap创建动态UI实现产品列表管理
2016/09/14 Javascript
jsp 自动编译机制详细介绍
2016/12/01 Javascript
JavaScript实现数值自动增加动画
2017/12/28 Javascript
Vue 中的compile操作方法
2018/02/26 Javascript
vue2过滤器模糊查询方法
2018/09/16 Javascript
JS如何在不同平台实现多语言方式
2020/07/16 Javascript
javaScript代码飘红报错看不懂?读完这篇文章再试试
2020/08/19 Javascript
Python做文本按行去重的实现方法
2016/10/19 Python
Python实现基于POS算法的区块链
2018/08/07 Python
Python生成器generator用法示例
2018/08/10 Python
ubuntu上安装python的实例方法
2019/09/30 Python
python 五子棋如何获得鼠标点击坐标
2019/11/04 Python
Python实现中值滤波去噪方式
2019/12/18 Python
TensorFlow使用Graph的基本操作的实现
2020/04/22 Python
Python直接赋值及深浅拷贝原理详解
2020/09/05 Python
在Pycharm中安装Pandas库方法(简单易懂)
2021/02/20 Python
使用HTML5做个画图板的方法介绍
2013/05/03 HTML / CSS
英国领先的互联网葡萄酒礼品商:Vintage Wine & Port
2019/05/24 全球购物
美国尼曼百货官网:Neiman Marcus
2019/09/05 全球购物
澳大利亚在线购买葡萄酒:The Wine Collective
2020/02/20 全球购物
科室工作个人总结的自我评价
2013/10/29 职场文书
大学生职业生涯规划书汇总
2014/03/20 职场文书
学习党代会心得体会
2014/09/05 职场文书
离职感谢信
2015/01/21 职场文书
副总经理岗位职责范本
2015/04/08 职场文书
python第三方网页解析器 lxml 扩展库与 xpath 的使用方法
2021/04/06 Python
Redis全局ID生成器的实现
2022/06/05 Redis
MySql中的json_extract函数处理json字段详情
2022/06/05 MySQL