网站首页 > 基础教程 正文
前几天学习的几个排序算法,C语言的实现。
这个是python版本的快速排序算法,使用了python的列表和模块numpy的数组格式,还对比了列表自身的sort方法和numba模块的加速耗时。
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
import numpy
import time
import copy
import math
import random
from numba import jit
from datetime import datetime
def TimeTest():
time.sleep(2)
''' 列表包含字典 下面是列表包含列表的实现
def QuickSort(arr):
n = len(arr)
r = [0]*(n)
p = 0
r[p] = {'start':0, 'end':n-1}
p += 1
while p:
p -= 1
rge = r[p];
if rge['start'] >= rge['end']:
continue
mid = arr[(rge['start'] + rge['end'])//2]; # 中间基点
left, right = rge['start'], rge['end']; # 两端开始
while (arr[left] < mid):
left += 1 # 基点左
while (arr[right] > mid):
right -= 1 # 基点右
if (left <= right):
arr[left],arr[right] = arr[right],arr[left]
left += 1
right -= 1
while (left <= right):
while (arr[left] < mid):
left += 1 # 基点左
while (arr[right] > mid):
right -= 1 # 基点右
if (left <= right):
arr[left],arr[right] = arr[right],arr[left]
left += 1
right -= 1
if (rge['start'] < right) :
r[p] = {'start':rge['start'], 'end':right}
p += 1
if (rge['end'] > left) :
r[p] = {'start':left, 'end':rge['end']}
p += 1
'''
def QuickSort(arr):
n = len(arr)
r = [0]*(n)
p = 0
r[p] = [0, n-1]
p += 1
while p:
p -= 1
rge = r[p];
if rge[0] >= rge[1]:
continue
mid = arr[(rge[0] + rge[1])//2]; # 中间基点
left, right = rge[0], rge[1]; # 两端开始
while (arr[left] < mid):
left += 1 # 基点左
while (arr[right] > mid):
right -= 1 # 基点右
if (left <= right):
arr[left],arr[right] = arr[right],arr[left]
left += 1
right -= 1
while (left <= right):
while (arr[left] < mid):
left += 1 # 基点左
while (arr[right] > mid):
right -= 1 # 基点右
if (left <= right):
arr[left],arr[right] = arr[right],arr[left]
left += 1
right -= 1
if (rge[0] < right) :
r[p] = [rge[0], right]
p += 1
if (rge[1] > left) :
r[p] = [left, rge[1]]
p += 1
@jit
def QuickSortsubjit(arr, rge):
mid = arr[(rge[0] + rge[1])//2]; # 中间基点
left, right = rge[0], rge[1]; # 两端开始
while (arr[left] < mid):
left += 1 # 基点左
while (arr[right] > mid):
right -= 1 # 基点右
if (left <= right):
arr[left],arr[right] = arr[right],arr[left]
left += 1
right -= 1
while (left <= right):
while (arr[left] < mid):
left += 1 # 基点左
while (arr[right] > mid):
right -= 1 # 基点右
if (left <= right):
arr[left],arr[right] = arr[right],arr[left]
left += 1
right -= 1
return left,right
def QuickSortjit(arr):
n = len(arr)
r = [0]*(n)
p = 0
r[p] = [0, n-1]
p += 1
while p:
p -= 1
rge = r[p];
if rge[0] >= rge[1]:
continue
left,right = QuickSortsubjit(arr, rge)
if (rge[0] < right) :
r[p] = [rge[0], right]
p += 1
if (rge[1] > left) :
r[p] = [left, rge[1]]
p += 1
start_ = datetime.utcnow()
TimeTest()
end_ = datetime.utcnow()
cdelt = (end_ - start_)
print("\t\t\t\t\t\t\t\t TimeTesto running time: %.3fms" % (cdelt.microseconds))
arrlist=[]
arrlen=16*1024
for i in range(arrlen):
arrlist.append(random.randint(0,32768))
arrlistjt = copy.deepcopy(arrlist)
arrlistso = copy.deepcopy(arrlist)
print ("排序self前的数组:")
for i in range(88):
print ('%0.6d ' % arrlist[i], end=" ")
start_ = datetime.utcnow()
QuickSort(arrlist)
end_ = datetime.utcnow()
cdelt = (end_ - start_)
print("\n排序self耗时: ====== ====== ====== ====== ====== ====== ====== == %.3fms" % (cdelt.microseconds))
print ("排序self后的数组:")
for i in range(88):
print ('%d ' % arrlist[i], end=" ")
########################################################################
print ("\n排序sort前的数组:")
for i in range(88):
print ('%d ' % arrlistso[i], end=" ")
start_ = datetime.utcnow()
arrlistso.sort(key=None, reverse=False)
end_ = datetime.utcnow()
cdelt = (end_ - start_)
print("\n排序sort耗时: ====== ====== ====== ====== ====== ====== ====== == %.3fms" % (cdelt.microseconds))
print ("排序sort后的数组:")
for i in range(88):
print ('%d ' % arrlistso[i], end=" ")
########################################################################
nparrlistjt=numpy.array(arrlistjt)
print ("\n排序npselfjitnp前的数组:")
for i in range(88):
print ('%d ' % nparrlistjt[i], end=" ")
start_ = datetime.utcnow()
QuickSortjit(nparrlistjt)
end_ = datetime.utcnow()
cdelt = (end_ - start_)
print("\n排序npselfjitnp耗时: ====== ====== ====== ====== ====== ====== == %.3fms" % (cdelt.microseconds))
print ("排序npselfjitnp后的数组:")
for i in range(88):
print ('%d ' % nparrlistjt[i], end=" ")
print ("\n==============================")
########################################################################
''' 运行不了了
print ("\n排序selfjit前的数组:")
for i in range(88):
print ('%d ' % arrlistjt[i], end=" ")
start_ = datetime.utcnow()
QuickSortjit(arrlistjt)
end_ = datetime.utcnow()
cdelt = (end_ - start_)
print("\n排序selfjit耗时: ====== ====== ====== ====== ====== ====== ====== %.3fms" % (cdelt.microseconds))
print ("排序selfjit后的数组:")
for i in range(88):
print ('%d ' % arrlistjt[i], end=" ")
print ("\n==============================")
'''
猜你喜欢
- 2025-01-06 Python数据结构与算法(13)——选择排序
- 2025-01-06 Python:pandas的DataFrame如何按指定list排序
- 2025-01-06 用Python实现列表排序,给定列表并进行升序降序排序(第一节)
- 2025-01-06 10个小技巧,让你的 Python 代码更加优雅
- 2025-01-06 算法浅谈——分治算法与归并、快速排序(附代码和动图演示)
- 2025-01-06 使用 Python 的sorted()函数对复杂可迭代对象进行排序
- 2025-01-06 一行Python代码:10个利用sort()函数解决复杂问题的案例
- 01-07Python从入门到放弃-详解列表、元组和字典
- 01-07python 中字典如何进行复制
- 01-07python入门023:字典嵌套
- 01-07掌握Python字典的12个例子
- 01-07使用Python 获取多级字典(Json)格式所有Key、Value
- 01-07简单学Python——字典的操作1(增加、更改和删除字典元素)
- 01-07Python之容器拾遗:Python就是包裹在一堆语法糖中的字典
- 01-07深入了解python字典的有序特性
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)