Python实现的几个常用排序算法实例

作者:junjie 时间:2021-08-13 07:42:54 

前段时间为准备百度面试恶补的东西,虽然最后还是被刷了,还是把那几天的“战利品”放点上来,算法一直是自己比较薄弱的地方,以后还要更加努力啊。

下面用Python实现了几个常用的排序,如快速排序,选择排序,以及二路并归排序等等。


#encoding=utf-8
import random
from copy import copy

def directInsertSort(seq):
 """ 直接插入排序 """
 size = len(seq)
 for i in range(1,size):
  tmp, j = seq[i], i
  while j > 0 and tmp < seq[j-1]:
   seq[j], j = seq[j-1], j-1
  seq[j] = tmp
 return seq

def directSelectSort(seq):
 """ 直接选择排序 """
 size = len(seq)
 for i in range(0,size - 1):
  k = i;j = i+1
  while j < size:
   if seq[j] < seq[k]:
    k = j
   j += 1
  seq[i],seq[k] = seq[k],seq[i]
 return seq

def bubbleSort(seq):
 """冒泡排序"""
 size = len(seq)
 for i in range(1,size):
  for j in range(0,size-i):
   if seq[j+1] < seq[j]:
    seq[j+1],seq[j] = seq[j],seq[j+1]
 return seq

def _divide(seq, low, high):
 """快速排序划分函数"""
 tmp = seq[low]
 while low != high:
  while low < high and seq[high] >= tmp: high -= 1
  if low < high:
   seq[low] = seq[high]
   low += 1
  while low < high and seq[low] <= tmp: low += 1
  if low < high:
   seq[high] = seq[low]
   high -= 1
 seq[low] = tmp
 return low

def _quickSort(seq, low, high):
 """快速排序辅助函数"""
 if low >= high: return
 mid = _divide(seq, low, high)
 _quickSort(seq, low, mid - 1)
 _quickSort(seq, mid + 1, high)

def quickSort(seq):
 """快速排序包裹函数"""
 size = len(seq)
 _quickSort(seq, 0, size - 1)
 return seq

def merge(seq, left, mid, right):
 tmp = []
 i, j = left, mid
 while i < mid and j <= right:
  if seq[i] < seq[j]:
   tmp.append(seq[i])
   i += 1
  else:
   tmp.append(seq[j])
   j += 1
 if i < mid: tmp.extend(seq[i:])
 if j <= right: tmp.extend(seq[j:])

 seq[left:right+1] = tmp[0:right-left+1]

def _mergeSort(seq, left, right):
 if left == right:
  return
 else:
  mid = (left + right) / 2
  _mergeSort(seq, left, mid)
  _mergeSort(seq, mid + 1, right)
  merge(seq, left, mid+1, right)

#二路并归排序
def mergeSort(seq):
 size = len(seq)
 _mergeSort(seq, 0, size - 1)
 return seq

if __name__ == '__main__':
 s = [random.randint(0,100) for i in range(0,20)]
 print s
 print "\n"
 print directSelectSort(copy(s))
 print directInsertSort(copy(s))
 print bubbleSort(copy(s))
 print quickSort(copy(s))
 print mergeSort(copy(s))
运行结果如下:


E:\python_project\practice>sorting.py
[10, 47, 56, 76, 64, 84, 26, 8, 47, 51, 88, 81, 32, 95, 91, 29, 28, 69, 61, 45]


[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]

标签:Python,排序算法
0
投稿

猜你喜欢

  • OpenCV半小时掌握基本操作之分水岭算法

    2023-04-10 11:24:51
  • php中防止SQL注入的最佳解决方法

    2023-08-19 21:52:20
  • mysql8.0.20数据目录迁移的方法

    2024-01-25 04:41:18
  • Python带动态参数功能的sqlite工具类

    2022-10-02 14:05:15
  • php异步:在php中使用fsockopen curl实现类似异步处理的功能方法

    2023-07-21 14:48:58
  • Pytorch实现WGAN用于动漫头像生成

    2023-07-24 22:31:26
  • 详解python中Numpy的属性与创建矩阵

    2023-08-29 03:35:17
  • Python数据分析基础之文件的读取

    2022-10-16 21:25:21
  • python3 使用OpenCV计算滑块拼图验证码缺口位置(场景示例)

    2023-02-27 07:07:18
  • 在Windows下安装配置CPU版的PyTorch的方法

    2023-07-18 15:04:17
  • MSSQL存储过程学习笔记一 关于存储过程

    2024-01-17 18:59:43
  • python定时器使用示例分享

    2023-04-06 15:03:57
  • Python实战之自动发送邮件的实现

    2023-10-18 15:09:43
  • python实现图像高斯金字塔的示例代码

    2023-05-06 02:02:32
  • 基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)

    2022-12-20 08:44:05
  • 浅谈MySQL中优化sql语句查询常用的30种方法

    2024-01-27 18:39:16
  • Mysql存储过程中游标的用法实例

    2024-01-22 14:59:10
  • python3人脸识别的两种方法

    2021-07-09 23:12:52
  • MySQL数据库事务原理及应用

    2024-01-17 13:02:49
  • 网页设计细节不能丢

    2007-09-26 11:54:00
  • asp之家 网络编程 m.aspxhome.com