我在Python3中做了一个排序算法,这个排序算法是一种改进的优化的3快速排序算法,它对大小为16的列表进行网络排序,并查找中值。我尝试过使用O(N)方法来找到中间值,但排序通常更快。我能得到一些关于如何使这件事更快的建议吗?另外,有人能链接到快速排序算法吗?我想把它们和我的比较,看看是否有更快的,如果它们是,我如何可以改进我的。第一个类似于1000行的是网络排序(我编写了一个自动生成网络排序代码的程序,我不是疯子),最后的20条代码正在测试它的速度。在我的笔记本电脑上,它在0.192秒内对10万个随机数进行排序,在2.754秒内对100万个随机数进行排序。(请注意,python的时间并不是100%准确的。我使用了多次测试,并找到了平均值。)
我已经将我的代码与合并、堆排序、其他快速排序、时间排序和基排序进行了比较,但它们似乎都无法超越我的代码。然而,这很可能是因为他们是从如何-to网站,而不是优化。遗憾的是,大多数关心快速排序算法的人似乎都是用Java、C或C++编写的,我实在无法与之相比。因此,我希望其他优化排序算法,甚至建议我的原始代码。
#importing
import math
import random
import time
#this is the sorting network (scroll a bunch, this is like 1000 lines)
def sorting_network(lst):
l = len(lst)
if l < 9:
if l < 5:
if l < 3:
if not l or l == 1:
return lst
else:
a = lst[0]
b = lst[1]
if a > b:
a, b = b, a
return [a, b]
else:
if l == 3:
a = lst[0]
b = lst[1]
c = lst[2]
if a > b:
a, b = b, a
if b > c:
b, c = c, b
if a > b:
a, b = b, a
return [a, b, c]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
if a > b:
a, b = b, a
if c > d:
c, d = d, c
if a > c:
a, c = c, a
if b > d:
b, d = d, b
if b > c:
b, c = c, b
return [a, b, c, d]
else:
if l < 7:
if l == 5:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
if a > d:
a, d = d, a
if b > e:
b, e = e, b
if a > b:
a, b = b, a
if c > e:
c, e = e, c
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if c > d:
c, d = d, c
return [a, b, c, d, e]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
if a > f:
a, f = f, a
if b > d:
b, d = d, b
if c > e:
c, e = e, c
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if a > d:
a, d = d, a
if c > f:
c, f = f, c
if a > b:
a, b = b, a
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if b > c:
b, c = c, b
if d > e:
d, e = e, d
return [a, b, c, d, e, f]
else:
if l == 7:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
if a > b:
a, b = a, b
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if a > c:
a, c = c, a
if b > e:
b, e = e, b
if d > g:
d, g = g, d
if a > b:
a, b = b, a
if c > f:
c, f = f, c
if d > e:
d, e = e, d
if b > c:
b, c = c, b
if e > g:
e, g = g, e
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if f > g:
f, g = g, f
return [a, b, c, d, e, f, g]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
if a > c:
a, c = c, a
if b > d:
b, d = d, b
if e > g:
e, g = g, e
if f > h:
f, h = h, f
if a > e:
a, e = e, a
if b > f:
b, f = f, b
if c > g:
c, g = g, c
if d > h:
d, h = h, d
if a > b:
a, b = b, a
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if c > e:
c, e = e, c
if d > f:
d, f = f, d
if b > e:
b, e = e, b
if d > g:
d, g = g, d
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if f > g:
f, g = g, f
return [a, b, c, d, e, f, g, h]
else:
if l < 13:
if l < 11:
if l == 9:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
if a > d:
a, d = d, a
if b > h:
b, h = h, b
if c > f:
c, f = f, c
if e > i:
e, i = i, e
if a > h:
a, h = h, a
if c > e:
c, e = e, c
if d > i:
d, i = i, d
if f > g:
f, g = g, f
if a > c:
a, c = c, a
if b > d:
b, d = d, b
if e > f:
e, f = f, e
if h > i:
h, i = i, h
if b > e:
b, e = e, b
if d > g:
d, g = g, d
if f > h:
f, h = h, f
if a > b:
a, b = b, a
if c > e:
c, e = e, c
if d > f:
d, f = f, d
if g > i:
g, i = i, g
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if f > g:
f, g = g, f
return [a, b, c, d, e, f, g, h, i]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
if a > i:
a, i = i, a
if b > j:
b, j = j, b
if c > h:
c, h = h, c
if d > f:
d, f = f, d
if e > g:
e, g = g, e
if a > c:
a, c = c, a
if b > e:
b, e = e, b
if f > i:
f, i = i, f
if h > j:
h, j = j, h
if a > d:
a, d = d, a
if c > e:
c, e = e, c
if f > h:
f, h = h, f
if g > j:
g, j = j, g
if a > b:
a, b = b, a
if d > g:
d, g = g, d
if i > j:
i, j = j, i
if b > f:
b, f = f, b
if c > d:
c, d = d, c
if e > i:
e, i = i, e
if g > h:
g, h = h, g
if b > c:
b, c = c, b
if d > f:
d, f = f, d
if e > g:
e, g = g, e
if h > i:
h, i = i, h
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if d > e:
d, e = e, d
if f > g:
f, g = g, f
return [a, b, c, d, e, f, g, h, i, j]
else:
if l == 11:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
k = lst[10]
if b > g:
b, g = g, b
if c > e:
c, e = e, c
if d > h:
d, h = h, d
if f > i:
f, i = i, f
if a > b:
a, b = b, a
if d > f:
d, f = f, d
if e > k:
e, k = k, e
if g > j:
g, j = j, g
if h > i:
h, i = i, h
if b > d:
b, d = d, b
if c > f:
c, f = f, c
if e > h:
e, h = h, e
if i > k:
i, k = k, i
if a > e:
a, e = e, a
if b > c:
b, c = c, b
if d > h:
d, h = h, d
if f > j:
f, j = j, f
if g > i:
g, i = i, g
if a > b:
a, b = b, a
if c > g:
c, g = g, c
if e > f:
e, f = f, e
if h > i:
h, i = i, h
if j > k:
j, k = k, j
if c > e:
c, e = e, c
if d > g:
d, g = g, d
if f > h:
f, h = h, f
if i > j:
i, j = j, i
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if f > g:
f, g = g, f
if h > i:
h, i = i, h
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
return [a, b, c, d, e, f, g, h, i, j, k]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
k = lst[10]
l = lst[11]
if b > h:
b, h = h, b
if c > g:
c, g = g, c
if d > l:
d, l = l, d
if e > k:
e, k = k, e
if f > j:
f, j = j, f
if a > b:
a, b = b, a
if c > f:
c, f = f, c
if d > e:
d, e = e, d
if g > j:
g, j = j, g
if h > i:
h, i = i, h
if k > l:
k, l = l, k
if a > c:
a, c = c, a
if b > g:
b, g = g, b
if f > k:
f, k = k, f
if j > l:
j, l = l, j
if a > d:
a, d = d, a
if b > c:
b, c = c, b
if e > g:
e, g = g, e
if f > h:
f, h = h, f
if i > l:
i, l = l, i
if j > k:
j, k = k, j
if b > e:
b, e = e, b
if d > f:
d, f = f, d
if g > i:
g, i = i, g
if h > k:
h, k = k, h
if b > d:
b, d = d, b
if c > f:
c, f = f, c
if g > j:
g, j = j, g
if i > k:
i, k = k, i
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if i > j:
i, j = j, i
if e > g:
e, g = g, e
if f > h:
f, h = h, f
if d > e:
d, e = e, d
if f > g:
f, g = g, f
if h > i:
h, i = i, h
return [a, b, c, d, e, f, g, h, i, k, l]
else:
if l < 15:
if l == 13:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
k = lst[10]
l = lst[11]
m = lst[12]
if a > m:
a, m = m, a
if b > k:
b, k = k, b
if c > j:
c, j = j, c
if d > h:
d, h = h, d
if f > l:
f, l = l, f
if g > i:
g, i = i, g
if b > g:
b, g = g, b
if c > d:
c, d = d, c
if e > l:
e, l = l, e
if h > j:
h, j = j, h
if i > k:
i, k = k, i
if a > e:
a, e = e, a
if b > c:
b, c = c, b
if d > g:
d, g = g, d
if h > i:
h, i = i, h
if j > k:
j, k = k, j
if l > m:
l, m = m, l
if e > g:
e, g = g, e
if f > j:
f, j = j, f
if i > l:
i, l = l, i
if k > m:
k, m = m, k
if a > f:
a, f = f, a
if d > i:
d, i = i, d
if e > h:
e, h = h, e
if g > l:
g, l = l, g
if j > k:
j, k = k, j
if a > b:
a, b = b, a
if c > f:
c, f = f, c
if g > j:
g, j = j, g
if h > i:
h, i = i, h
if k > l:
k, l = l, k
if b > d:
b, d = d, b
if c > e:
c, e = e, c
if f > g:
f, g = g, f
if j > k:
j, k = k, j
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if f > h:
f, h = h, f
if g > i:
g, i = i, g
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if i > j:
i, j = j, i
if d > e:
d, e = e, d
if f > g:
f, g = g, f
return [a, b, c, d, e, f, g, h, i, j, k, l, m]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
k = lst[10]
l = lst[11]
m = lst[12]
n = lst[13]
if a > g:
a, g = g, a
if b > l:
b, l = l, b
if c > m:
c, m = m, c
if d > k:
d, k = k, d
if e > f:
e, f = f, e
if h > n:
h, n = n, h
if i > j:
i, j = j, i
if b > c:
b, c = c, b
if d > h:
d, h = h, d
if e > i:
e, i = i, e
if f > j:
f, j = j, f
if g > k:
g, k = k, g
if l > m:
l, m = m, l
if a > e:
a, e = e, a
if b > d:
b, d = d, b
if f > g:
f, g = g, f
if h > i:
h, i = i, h
if j > n:
j, n = n, j
if k > m:
k, m = m, k
if a > b:
a, b = b, a
if c > j:
c, j = j, c
if d > h:
d, h = h, d
if e > l:
e, l = l, e
if g > k:
g, k = k, g
if m > n:
m, n = n, m
if c > f:
c, f = f, c
if e > h:
e, h = h, e
if g > j:
g, j = j, g
if i > l:
i, l = l, i
if b > c:
b, c = c, b
if d > e:
d, e = e, d
if g > h:
g, h = h, g
if j > k:
j, k = k, j
if l > m:
l, m = m, l
if b > d:
b, d = d, b
if c > e:
c, e = e, c
if f > g:
f, g = g, f
if h > i:
h, i = i, h
if j > l:
j, l = l, j
if k > m:
k, m = m, k
if c > d:
c, d = d, c
if e > h:
e, h = h, e
if g > j:
g, j = j, g
if k > l:
k, l = l, k
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if i > j:
i, j = j, i
if d > e:
d, e = e, d
if f > g:
f, g = g, f
if h > i:
h, i = i, h
if j > k:
j, k = k, j
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n]
else:
if l == 15:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
k = lst[10]
l = lst[11]
m = lst[12]
n = lst[13]
o = lst[14]
if b > c:
b, c = c, b
if d > k:
d, k = k, d
if e > o:
e, o = o, e
if f > i:
f, i = i, f
if g > n:
g, n = n, g
if h > m:
h, m = m, h
if j > l:
j, l = l, j
if a > o:
a, o = o, a
if b > f:
b, f = f, b
if c > i:
c, i = i, c
if d > h:
d, h = h, d
if g > j:
g, j = j, g
if k > m:
k, m = m, k
if l > n:
l, n = n, l
if a > h:
a, h = h, a
if b > g:
b, g = g, b
if c > j:
c, j = j, c
if e > k:
e, k = k, e
if f > l:
f, l = l, f
if i > n:
i, n = n, i
if m > o:
m, o = o, m
if a > g:
a, g = g, a
if c > e:
c, e = e, c
if d > f:
d, f = f, d
if h > l:
h, l = l, h
if i > k:
i, k = k, i
if j > m:
j, m = m, j
if n > o:
n, o = o, n
if a > d:
a, d = d, a
if b > c:
b, c = c, b
if e > h:
e, h = h, e
if f > j:
f, j = j, f
if g > i:
g, i = i, g
if k > l:
k, l = l, k
if m > n:
m, n = n, m
if a > b:
a, b = b, a
if c > d:
c, d = d, c
if e > g:
e, g = g, e
if h > j:
h, j = j, h
if k > m:
k, m = m, k
if l > n:
l, n = n, l
if b > c:
b, c = c, b
if d > f:
d, f = f, d
if i > k:
i, k = k, i
if l > m:
l, m = m, l
if d > e:
d, e = e, d
if f > g:
f, g = g, f
if h > i:
h, i = i, h
if j > k:
j, k = k, j
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > h:
g, h = h, g
if i > j:
i, j = j, i
if k > l:
k, l = l, k
if f > g:
f, g = g, f
if h > i:
h, i = i, h
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o]
else:
a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]
e = lst[4]
f = lst[5]
g = lst[6]
h = lst[7]
i = lst[8]
j = lst[9]
k = lst[10]
l = lst[11]
m = lst[12]
n = lst[13]
o = lst[14]
p = lst[15]
if a > n:
a, n = n, a
if b > m:
b, m = m, b
if c > p:
c, p = p, c
if d > o:
d, o = o, d
if e > i:
e, i = i, e
if f > g:
f, g = g, f
if h > l:
h, l = l, h
if j > k:
j, k = k, j
if a > f:
a, f = f, a
if b > h:
b, h = h, b
if c > j:
c, j = j, c
if d > e:
d, e = e, d
if g > n:
g, n = n, g
if i > o:
i, o = o, i
if k > p:
k, p = p, k
if l > m:
l, m = m, l
if a > b:
a, b = b, a
if c > d:
c, d = d, c
if e > f:
e, f = f, e
if g > i:
g, i = i, g
if h > j:
h, j = j, h
if k > l:
k, l = l, k
if m > n:
m, n = n, m
if o > p:
o, p = p, o
if a > c:
a, c = c, a
if b > d:
b, d = d, b
if e > k:
e, k = k, e
if f > l:
f, l = l, f
if g > h:
g, h = h, g
if i > j:
i, j = j, i
if m > o:
m, o = o, m
if n > p:
n, p = p, n
if b > c:
b, c = c, b
if d > m:
d, m = m, d
if e > g:
e, g = g, e
if f > h:
f, h = h, f
if i > k:
i, k = k, i
if j > l:
j, l = l, j
if n > o:
n, o = o, n
if b > e:
b, e = e, b
if c > g:
c, g = g, c
if f > i:
f, i = i, f
if h > k:
h, k = k, h
if j > n:
j, n = n, j
if l > o:
l, o = o, l
if c > e:
c, e = e, c
if d > g:
d, g = g, d
if j > m:
j, m = m, j
if l > n:
l, n = n, l
if d > f:
d, f = f, d
if g > i:
g, i = i, g
if h > j:
h, j = j, h
if k > m:
k, m = m, k
if d > e:
d, e = e, d
if f > g:
f, g = g, f
if h > i:
h, i = i, h
if j > k:
j, k = k, j
if l > m:
l, m = m, l
if g > h:
g, h = h, g
if i > j:
i, j = j, i
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]
#and this is the quicksort algorithm!
def swiftsort(lst):
l = len(lst)
if l < 17:
return sorting_network(lst)
elif l < 128:
a = lst[0]
b = lst[l // 2]
c = lst[-1]
if a <= b <= c or c <= b <= a:
split = b
elif b <= c <= a or a <= c <= b:
split = c
else:
split = a
elif l < 8192:
a = [lst[0], lst[l // 8], lst[2 * l // 8], lst[3 * l // 8], lst[4 * l // 8], lst[5 * l // 8], lst[6 * l // 8], lst[7 * l // 8], lst[-1]]
split = sorting_network(lst)[4]
else:
a = [lst[0]]
for x in range(1, 32):
a.append(lst[x * l // 32])
a.append(lst[-1])
split = swiftsort(a)[16]
lst1 = []
lst2 = []
mid = []
for x in lst:
if x < split:
lst1.append(x)
elif x == split:
mid.append(x)
else:
lst2.append(x)
return swiftsort(lst1) + mid + swiftsort(lst2)
#This is for testing the time of my algorithm.
length = 100000
runs = 60
test_list = [random.randint(1, length) for _ in range(length)]
t = 0
for _ in range(runs):
start = time.time()
sort = swiftsort(test_list)
end = time.time()
t += end - start
print(t / runs)发布于 2021-03-22 13:24:01
(首先是对索拉天空显示的分区和网络排序编码方式的看法。我打算谈谈对资源需求的评论--不要屏住呼吸。
在你的问题中,你提供了背景:什么和为什么。在代码中这样做!Python提供文档字符串是正确的,这是一种文档机制,成功地使代码不可能与文档分离。(它甚至可以用于内省。)您可能会发现自己想在编写sorting_network()的docstring之后重命名它。文档字符串功能在Python代码样式指南中,您似乎知道这一点。它包含了一些好的建议,比如评论是不必要的,事实上,如果他们说明显的- #importing是一个非评论的话,就会分散注意力。目前不使用math。(random和time在基准测试中“只是”使用,稍后会有更多介绍。)
sorting_network(lst):我注意到您没有使用类型提示。在“决策树”中,不需要在else之后使用return。没有代码缩进的好处之一是减少代码缩进。我本想在代码中读到关于生成器构造的比较网络的类型--我没有试图弄清楚,对于使用最优深度或比较器的网络没有评论(/comparisons?)这里。我的IDE“数”60表示len 16 --我发现这是最有名的。
(and this is the quicksort algorithm!用于分割头发,我希望快速排序能够就地排序(希望输入的大小会增加空间)。) swiftsort(lst): return-else,再一次“习惯性地”,快速排序是用枢轴选择和分区分解编码的。“比较”非常容易读-- split = b if a <= b == b <= c else c if b <= c == c <= a else a重复一个比较,而不是潜在的四个比较。在理解上更喜欢for,而不是枚举元素或在循环中追加元素:[lst[l_ * i // 8] for i in range(9) for l_ in (l - 1,)] (第二个for刚刚引入了l_ (=l-1)“内联”)“8192-限制”看起来有点武断--使用比ls表示中的位数少一个项目怎么样?
samples = (int.bit_length(l) // 2) * 2 - 1
samples = [lst[(l - 1) * i // (samples - 1)] for i in range(samples)]
pivot = sorted(samples)[len(samples)//2](我可以写(int.bit_length(l)&~1) -我能读吗?你?维护程序员?)(对于不超过16项的项目,swiftsort()/sorting_network()看起来更有利。在上面不太确定)(哦,你看,你也可以废除“128比较”。)
虽然lst是一个这么简单的名称,但lst1/lst2只是不这么做:为了简洁起见,我更喜欢before&after而不是preceding&following,对于不解释顺序(您可能希望通过诸如可选比较器或reverse标记,比如sorted())的机制来重新定义该顺序。在为此提供更短语法的语言中,我可能更倾向于使用等效的(before if x < split else mid if x == split else after).append(x)来强调区别在于选择序列,而不是对其或参数进行操作。在“生产强度排序”中,我会尽量避免最坏情况下的资源消耗:在较短的before&after上递归,在另一个上迭代。用三向分区看起来一点也不简单。
对于文件中的Python代码,有命名模块:模块名是“没有尾随的.py”的文件名。python解释器开始执行的模块在执行过程中名为"__main__"。这允许在用于库样式的代码中保留简短的测试或基准测试:
# This is for testing the time of my algorithm.
if __name__== '__main__':
import random # place "non-library imports" here
# some test code评估资源使用的一般预期和时间,特别是微基准是有趣的蠕虫罐。让我提一下时差和Cython。您选择的问题大小越大,就越适合当代通用处理器的(L3)缓存。
https://codereview.stackexchange.com/questions/257486
复制相似问题