Python 希尔排序
最后更新于:2022-03-27 02:37:37
Python 希尔排序
希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本。但希尔排序是非稳定排序算法。
希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录”基本有序”时,再对全体记录进行依次直接插入排序。
实例
def shellSort(arr):
gap = int(n/2)
j = i
while j >= gap and arr[j–gap] >temp:
arr[j] = arr[j–gap]
j -= gap
arr[j] = temp
gap = int(gap/2) arr = [ 12, 34, 54, 2, 3] n = len(arr)
print ("排序前:")
for i in range(n):
print(arr[i]), shellSort(arr) print ("\n排序后:")
for i in range(n):
print(arr[i]),
n = len(arr)
gap = int(n/2)
while gap > 0:
for i in range(gap,n):
temp = arr[i]
j = i
while j >= gap and arr[j–gap] >temp:
arr[j] = arr[j–gap]
j -= gap
arr[j] = temp
gap = int(gap/2) arr = [ 12, 34, 54, 2, 3] n = len(arr)
print ("排序前:")
for i in range(n):
print(arr[i]), shellSort(arr) print ("\n排序后:")
for i in range(n):
print(arr[i]),
执行以上代码输出结果为:
排序前: 12 34 54 2 3 排序后: 2 3 12 34 54