Python实现排序方法常见的四种

作者:小小程序员ol 时间:2022-02-18 08:06:15 

1.冒泡排序,相邻位置比较大小,将比较大的(或小的)交换位置


def maopao(a):
   for i in range(0,len(a)):
       for j in range(0,len(a)-i-1):
           if a[j]>a[j+1]:
               temp = a[j+1]
               a[j+1] = a[j]
               a[j] = temp
               #print(a)
       #print(a)
   print(a)

2.选择排序,遍历选择一个最小的数与当前循环的第一个数交换


def xuanze(a):
   for i in range(0,len(a)):
       k=i
       temp = a[i]
       for j in range(i,len(a)):
           if a[j]<temp:
               temp = a[j]
               k = j
       a[k] = a[i]
       a[i] = temp
   print(a)

3.快速排序:将子段的第一个元素做为中值,先从右向左遍历,如过比中值大high-1,如果比中值小,将这个值放到low那里。

然后从左向右开始遍历,如果左侧的比中值大,将他放到high那里。当low>=high时,将中值的值赋给low

(1.以下为参照公众号中的做法:


a =[7,1,3,2,6,54,4,4,5,8,12,34]
def sort(a,low,high):
   while low < high:
       temp = a[low]
       while low < high and a[high]>=temp:
           high = high-1
       a[low]=a[high]
       while low<high and a[low]<temp:
           low = low+1
       a[high]=a[low]          
       a[low]=temp
   return low
def quicksort(a,low,high):
   if low<high:
       middle = sort(a,low,high)
       quicksort(a,low,middle)
       quicksort(a,middle+1,high)
       print(a)
sort(a,0,len(a)-1)
quicksort(a,0,len(a)-1)
print(a)

(2.以下是参照网上的做法:

在做快速排序时一直各种问题,是因为地柜那里没有考虑清楚,一直把low的值赋值为0了,实际上应该是不固定的low值,他每个子循环不定。


'''
遇到问题没人解答?小编创建了一个Python学习交流群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
a =[7,1,3,2,6,54,4,4,5,8,12,34]
def sort(a,low,high):
   while low < high:
       temp = a[low]
       while low < high and a[high]>=temp:
           high = high-1
       while low<high and a[high]<temp:
           a[low]=a[high]          
           low =low+1
           a[high]=a[low]
       a[low]=temp
   return low
def quicksort(a,low,high):
   if low<high:
       middle = sort(a,low,high)
       quicksort(a,low,middle)
       quicksort(a,middle+1,high)
       print(a)
sort(a,0,len(a)-1)
quicksort(a,0,len(a)-1)
print(a)

4.插入排序:从左向右遍历,依次选取数值,从数值的左侧从右向左遍历,选择第一个比他小的数值的右侧插入该数值,其他数值依次向后赋值


#插入排序
a =[7,1,3,2,6,54,4,4,5,8,12,34]
for i in range(0,len(a)-1):
   temp=a[i+1]
   j=i+1
   while j>=0 and temp<a[j-1]:
       j=j-1      
       print(j)
   if j>=-1:
       k= i+1
       while k>=j:
           a[k]=a[k-1]
           k=k-1
           print(a)
       a[j]=temp
print(a)

插入排序方法2,用到了列表的a.insert(1,2)和清楚a[2:3]=[],这样可以少用一个循环


a =[7,1,3,2,6,54,4,4,5,8,12,34]
for i in range(1,len(a)-1):
   temp=a[i]

j=i-1
   while j>=0 and temp<=a[j]:
       print(temp)
       j=j-1
   if j >=-1:
       a[i:i+1]=[]
       a.insert(j+1,temp)
       print(a)
print(a)

来源:https://www.cnblogs.com/python960410445/p/15013047.html

标签:python,排序方法
0
投稿

猜你喜欢

  • css网页下拉菜单制作方法(1):基本原理

    2007-02-03 11:39:00
  • php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法

    2023-07-16 11:36:01
  • 25个出色的使用叶子的logo设计

    2009-12-29 12:53:00
  • vertical-align表单元素垂直对齐

    2009-07-27 13:02:00
  • 百度百科的图片轮播代码

    2009-05-06 12:58:00
  • document.createElement()用法及注意事项

    2008-04-21 15:16:00
  • selenium+python实现文件上传操作的方法实例

    2022-05-06 13:21:49
  • Swoole webSocket消息服务系统代码设计详解

    2023-06-09 01:05:28
  • 重新编译PLSQL中的无效对象或者指定的对象 的方法

    2009-02-26 10:41:00
  • python神经网络Keras GhostNet模型的实现

    2022-05-16 17:19:42
  • Python对比校验神器deepdiff库使用详解

    2023-05-14 11:05:35
  • php下载文件源代码(强制任意文件格式下载)

    2023-10-10 07:53:20
  • 解决TensorFlow调用Keras库函数存在的问题

    2023-11-20 15:04:25
  • JavaScript解释型模版

    2009-10-19 23:12:00
  • 使用matplotlib绘制图例标签中带有公式的图

    2022-07-19 00:40:48
  • 客户端数据存储–超越cookies

    2008-01-15 13:01:00
  • Python切片知识解析

    2022-06-07 06:31:58
  • python中slice参数过长的处理方法及实例

    2023-07-13 13:12:10
  • 小诀窍让你快速上手Dreamweaver

    2007-12-03 11:35:00
  • Python时间获取及转换知识汇总

    2023-08-02 12:17:08
  • asp之家 网络编程 m.aspxhome.com