python实现排序算法解析


Posted in Python onSeptember 08, 2018

本文实例为大家分享了python实现排序算法的具体代码,供大家参考,具体内容如下

一、冒泡排序

def bububle_sort(alist):
 """冒泡排序(稳定|n^2m)"""
 n = len(alist)
 for j in range(n-1):
  count = 0
  for i in range(0,n-1-j):
   if alist[i]>alist[i+1]:
    count +=1
    alist[i], alist[i+1] = alist[i+1], alist[i]
  if count==0:
   return

二、选择排序

def select_sort(alist):
  """选择排序(不稳定|n^2)"""
  n = len(alist)
  for j in range(n-1):
    min_index = j
    for i in range(j+1,n):
      if alist[min_index] > alist[i]:
        min_index = i
    alist[j], alist[min_index] = alist[min_index], alist[j]

三、插入排序

def insert_sort(alist):
  """插入排序(稳定|n^2)"""
  n = len(alist)
  for j in range(1,n):
    i = j
    while i>0:
      if alist[i] < alist[i-1]:
        alist[i], alist[i-1] = alist[i-1], alist[i]
        i -= 1
      else:
        break

四、希尔排序

def shell_sort(alist):
  """希尔排序(不稳定|n^2)"""
  n = len(alist)
  gap = n//2

  while gap>=1:
    for j in range(gap,n):
      i=j
      while i>0:
        if alist[i]<alist[i-gap]:
          alist[i], alist[i-gap] = alist[i-gap], alist[i]
          i -= gap
        else:
          break
    gap //=2

五、快速排序

def quick_sort(alist, first, last):
  """快速排序(不稳定|n^2)"""
  if first >= last:
    return
  mid_value = alist[first]
  low = first
  high = last
  while low < high:
    #high左移
    while low <high and alist[high] >= mid_value:
      high -= 1
    alist[low] = alist[high]
    #low右移
    while low < high and alist[low] < mid_value:
      low += 1
    alist[high] =alist[low] 
  #从循环退出时,low=high
  alist[low] = mid_value

  #对low左边的列表执行快速排序
  quick_sort(alist, first, low-1)
  #对low右边的列表执行快速排序
  quick_sort(alist, low+1, last)

六、归并排序

def merge_sort(alist):
  """归并排序(稳定|nlgn)"""
  n = len(alist)
  if n <= 1:
    return alist
  mid = n//2

  #left 采用归并排序后形成新的有序列表
  left_li = merge_sort(alist[:mid])
  #right 采用归并排序后形成新的有序列表
  right_li = merge_sort(alist[mid:])

  #merge(left, right) 将两个有序的子序列合并为一个新的整体
  left_pointer, right_pointer = 0, 0
  result = []

  while left_pointer < len(left_li) and right_pointer<len(right_li):
    if left_li[left_pointer] < right_li[right_pointer]:
      result.append(left_li[left_pointer])
      left_pointer += 1
    else:
      result.append(right_li[right_pointer])
      right_pointer += 1

  result += left_li[left_pointer:]
  result += right_li[right_pointer:]
  return result

python实现排序算法解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中文乱码的解决方法
Nov 04 Python
python 动态获取当前运行的类名和函数名的方法
Apr 15 Python
Python3使用requests发闪存的方法
May 11 Python
Pytorch入门之mnist分类实例
Apr 14 Python
python 用正则表达式筛选文本信息的实例
Jun 05 Python
对django中render()与render_to_response()的区别详解
Oct 16 Python
在Python中append以及extend返回None的例子
Jul 20 Python
Django中的FBV和CBV用法详解
Sep 15 Python
pytorch实现从本地加载 .pth 格式模型
Feb 14 Python
使用Keras加载含有自定义层或函数的模型操作
Jun 10 Python
matplotlib绘制多子图共享鼠标光标的方法示例
Jan 08 Python
Python中np.random.randint()参数详解及用法实例
Sep 23 Python
TensorFlow实现Logistic回归
Sep 07 #Python
tensorflow实现简单逻辑回归
Sep 07 #Python
Tensorflow使用支持向量机拟合线性回归
Sep 07 #Python
TensorFlow实现iris数据集线性回归
Sep 07 #Python
TensorFlow实现模型评估
Sep 07 #Python
使用tensorflow实现线性svm
Sep 07 #Python
Python多进程池 multiprocessing Pool用法示例
Sep 07 #Python
You might like
PHP 的 __FILE__ 常量
2007/01/15 PHP
浅析PHP的ASCII码转换类
2013/07/05 PHP
php 生成加密公钥加密私钥实例详解
2017/06/16 PHP
Windows平台PHP+IECapt实现网页批量截图并创建缩略图功能详解
2019/08/02 PHP
IE6 fixed的完美解决方案
2011/03/31 Javascript
js获取时间(本周、本季度、本月..)
2013/11/22 Javascript
JS获得QQ号码的昵称,头像,生日的简单实例
2013/12/04 Javascript
js实现字符串的16进制编码不加密
2014/04/25 Javascript
JavaScript判断undefined类型的正确方法
2015/06/30 Javascript
Angular2 环境配置详细介绍
2016/09/21 Javascript
Node.js制作简单聊天室
2017/01/12 Javascript
JS使用ActiveXObject实现用户提交表单时屏蔽敏感词功能
2017/06/20 Javascript
Angular.js通过自定义指令directive实现滑块滑动效果
2017/10/13 Javascript
vue综合组件间的通信详解
2017/11/06 Javascript
浅谈ES6 模板字符串的具体使用方法
2017/11/07 Javascript
webpack将js打包后的map文件详解
2018/02/22 Javascript
微信小程序实现炫酷的弹出式菜单特效
2019/01/28 Javascript
详解小程序之简单登录注册表单验证
2019/05/13 Javascript
微信小程序利用Canvas绘制图片和竖排文字详解
2019/06/25 Javascript
jQuery 隐藏/显示效果函数用法实例分析
2020/05/20 jQuery
Python使用wget实现下载网络文件功能示例
2018/05/31 Python
python 3调用百度OCR API实现剪贴板文字识别
2018/09/04 Python
python爬虫简单的添加代理进行访问的实现代码
2019/04/04 Python
在Python中COM口的调用方法
2019/07/03 Python
Series和DataFrame使用简单入门
2019/11/13 Python
浅谈盘点5种基于Python生成的个性化语音方法
2021/02/05 Python
HTML5里的placeholder属性使用实例和美化显示效果的方法
2014/04/23 HTML / CSS
关于h5中的fetch方法解读(小结)
2017/11/15 HTML / CSS
html5跳转小程序wx-open-launch-weapp踩坑
2020/12/02 HTML / CSS
外贸采购员求职的自我评价
2013/11/26 职场文书
外国人聘用意向书
2014/04/01 职场文书
2014年安全生产大检查方案
2014/05/13 职场文书
个人批评与自我批评范文
2014/10/17 职场文书
七一活动主持词
2015/06/29 职场文书
详解Nginx启动失败的几种错误处理
2021/04/01 Servers
用Python监控你的朋友都在浏览哪些网站?
2021/05/27 Python