python选择排序算法实例总结

 更新时间:2015年07月01日 11:17:18   作者:pythoner  
这篇文章主要介绍了python选择排序算法,以三个实例以不同方法分析了Python实现选择排序的相关技巧,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例总结了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爬虫用mongodb的理由

    python爬虫用mongodb的理由

    在本篇内容中小编给大家整理的是关于python爬虫用mongodb的理由,需要的朋友们可以跟着学习参考下。
    2020-07-07
  • Django异步任务之Celery的基本使用

    Django异步任务之Celery的基本使用

    这篇文章主要给大家介绍了关于Django异步任务之Celery使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Django具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Python装饰器实现几类验证功能做法实例

    Python装饰器实现几类验证功能做法实例

    下面小编就为大家带来一篇Python装饰器实现几类验证功能做法实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • pyinstaller打包后偶尔出现黑窗口一闪而过的问题及解决

    pyinstaller打包后偶尔出现黑窗口一闪而过的问题及解决

    这篇文章主要介绍了pyinstaller打包后偶尔出现黑窗口一闪而过的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Python多线程多进程实例对比解析

    Python多线程多进程实例对比解析

    这篇文章主要介绍了Python多线程多进程实例对比解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • python数组转换为矩阵的方法实现

    python数组转换为矩阵的方法实现

    本文主要介绍了python数组转换为矩阵的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • django 创建过滤器的实例详解

    django 创建过滤器的实例详解

    这篇文章主要介绍了django 创建过滤器的实例详解的相关资料,主要说明django 创建过滤器来统一处理字符串,需要的朋友可以参考下
    2017-08-08
  • Python?中将数字转换为字母的方法

    Python?中将数字转换为字母的方法

    本文详细介绍了在 Python 中将数字转换为字母的几种常用方法,我们介绍了使用 chr() 函数、string 模块和 ord() 函数等方法,并提供了示例代码帮助你理解和应用这些方法,感兴趣的朋友跟随小编一起看看吧
    2023-06-06
  • Python自动化办公之定时发送邮件的实现

    Python自动化办公之定时发送邮件的实现

    python中的schedule模块可以使我们方便简单的使用定时任务,即在特定的时间自动的执行一些任务的功能,本文将用这一模块实现邮件自动发送,需要的可以参考一下
    2022-05-05
  • Python实现视频分解成图片+图片合成视频

    Python实现视频分解成图片+图片合成视频

    这篇文章主要介绍了如何利用Python实现视频分解成图片以及将图片合成为视频,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-04-04

最新评论

?


http://www.vxiaotou.com