python希尔排序的优缺点_Pythonの希尔排序
1.[代碼][Python]代碼
# -*- coding: utf-8 -*-
def step_half(n):
"""Original sequence: n/2, n/4, ..., 1
a(n) = ceil(n/(2^k)), k >= 1
原始步長(zhǎng):從n/2開(kāi)始不斷減半直至1。
"""
while True:
n = n / 2
v = int(n)
yield v
if v <= 1:
return
def _step_sedgewick():
"""Sedgewick's increments: 1, 5, 19, 41, 109, ...
1, 19, 109, 505, 2161, ..., 9(4^k – 2^k) + 1, k = 0, 1, 2, 3, …
5, 41, 209, 929, 3905, ..., 2^(k+2) (2^(k+2) – 3) + 1, k = 0, 1, 2, 3, …
Sedgewick提出的步長(zhǎng)序列:用這樣步長(zhǎng)串行的希爾排序比插入排序和堆排序都要快,
甚至在小數(shù)組中比快速排序還快,但是在涉及大量數(shù)據(jù)時(shí)希爾排序還是比快速排序慢。
"""
def step_a():
k = 0
while True:
yield 9 * (4 ** k - 2 ** k) + 1
k += 1
def step_b():
k = 0
while True:
v = 2 ** (k + 2)
yield v * (v - 3) + 1
k += 1
gen_a, gen_b = step_a(), step_b()
a, b = gen_a.next(), gen_b.next()
small = min(a, b)
yield small
while True:
if small == a:
a = gen_a.next()
else:
b = gen_b.next()
small = min(a, b)
yield small
def step_sedgewick(n):
"""限制步長(zhǎng)序列值小于n"""
gen_step = _step_sedgewick()
while True:
v = gen_step.next()
if v < n:
yield v
else:
return
def shell_sort(A, gaps):
"""希爾排序,偽碼如下:
SHELL-SORT(A, gaps)
1 n ← length[A] // 數(shù)組長(zhǎng)度
2 foreach gap in gaps
3 ? 為每個(gè)步長(zhǎng)下各列做插入排序
4 for i ← gap to n-1 // 從步長(zhǎng)位至末位
5 do temp ← A[i]
6 j ← i
7 while j >= gap and A[j - gap] > temp // 與步長(zhǎng)前一位比較,直至不小于它
8 do A[j] ← A[j - gap] // 大值后移
9 j ← j - gap // 再前一位
10 A[j] = temp
Args:
A (Sequence): 數(shù)組
gaps (Iterator): 步長(zhǎng)序列
更多步長(zhǎng):http://en.wikipedia.org/wiki/Shellsort
"""
n = len(A)
for gap in gaps:
for i in xrange(gap, n):
temp = A[i]
j = i
while j >= gap and A[j - gap] > temp:
A[j] = A[j - gap]
j = j - gap
A[j] = temp
if __name__ == '__main__':
import random, timeit
items = range(10000)
random.shuffle(items)
def test_sorted():
# print(items)
sorted_items = sorted(items)
# print(sorted_items)
# _gaps = step_half(len(items))
_gaps = reversed([a for a in step_sedgewick(len(items))])
def test_shell_sort():
# print(items)
shell_sort(items, _gaps)
# print(items)
test_methods = [test_sorted, test_shell_sort]
for test in test_methods:
name = test.__name__ # test.func_name
t = timeit.Timer(name + '()', 'from __main__ import ' + name)
print(name + ' takes time : %f' % t.timeit(1))
總結(jié)
以上是生活随笔為你收集整理的python希尔排序的优缺点_Pythonの希尔排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: a*算法的优缺点_五种聚类算法一览与py
- 下一篇: python非阻塞输入_python_非