python选择排序算法实例总结

 更新时间:2015年07月01日 11:17:18   作者:pythoner  
这篇文章主要介绍了python选择排序算法,以三个实例以不同方法分析了Python实现选择排序的相关技巧,需要的朋友可以参考下

本文实例总结了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中numpy数组真值判断的实现

    Python中numpy数组真值判断的实现

    在Python编程中,经常需要对数组进行真值判断,本文就来介绍一下Python中numpy数组真值判断的实现,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • Python GUI编程 文本弹窗的实例

    Python GUI编程 文本弹窗的实例

    今天小编就为大家分享一篇Python GUI编程 文本弹窗的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • 基于Python实现的恋爱对话小程序详解

    基于Python实现的恋爱对话小程序详解

    这篇文章主要介绍了基于Python制作一个恋爱对话小程序,文章详细介绍了小程序的实现过程,感兴趣的小伙伴可以跟随小编一起学习学习
    2022-01-01
  • Pandas实现Excel文件读取,增删,打开,保存操作

    Pandas实现Excel文件读取,增删,打开,保存操作

    Pandas 是一种基于 NumPy 的开源数据分析工具,用于处理和分析大量数据。本文将通过Pandas实现对Excel文件进行读取、增删、打开、保存等操作,需要的可以参考一下
    2023-04-04
  • Pycharm添加虚拟解释器报错问题解决方案

    Pycharm添加虚拟解释器报错问题解决方案

    这篇文章主要介绍了Pycharm添加虚拟解释器报错问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • django有哪些好处和优点

    django有哪些好处和优点

    在本篇内容里小编给大家整理的是一篇关于django有哪些好处和优点的相关内容,有需要的朋友们可以参考下。
    2020-09-09
  • 浅谈Python之Django(二)

    浅谈Python之Django(二)

    这篇文章主要介绍了Python3中的Django,小编觉得这篇文章写的还不错,需要的朋友们下面随着小编来一起学习学习吧,希望能够给你带来帮助
    2021-10-10
  • 基于PyQt5完成的PDF拆分功能

    基于PyQt5完成的PDF拆分功能

    这篇文章主要介绍了基于PyQt5完成的PDF拆分功能,本文介绍的pdf拆分功能还有一些待完善地方,例如可增加预览功能,实现每页预览,以及如何实现多条件拆分,需要的朋友可以参考下
    2022-06-06
  • Python 使用openpyxl处理Excel文件详情

    Python 使用openpyxl处理Excel文件详情

    这篇文章主要介绍了Python 使用openpyxl处理Excel文件详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Python实现给文件添加内容及得到文件信息的方法

    Python实现给文件添加内容及得到文件信息的方法

    这篇文章主要介绍了Python实现给文件添加内容及得到文件信息的方法,可实现从文件开头添加内容的功能,需要的朋友可以参考下
    2015-05-05

最新评论