一、基本性质总结
定义 | 希尔排序 | |
稳定 | 如果a原本在b前面,而a=b,排序之后a仍然在b前面 | 不稳定 |
内排序 | 所有排序操作都在内存中完成 | 是 |
外排序 | 由于数据太大,因此把数据放在磁盘中,而排序通过磁盘和内存的数据传输才能进行 | 否 |
时间复杂度 | 算法执行所耗费的时间 | |
空间复杂度 | 运行完一个程序所需内存的大小 | |
二、基本思想
取增量为a的数据进行分组,分别对每组进行直接插入排序,即元素位置的交换是跳跃式,分组比较完后再按间隔a/2(向上取整)进行比较,,直至当增量a=1时,序列已基本有序,按直接插入排序对整个序列调整,结束排序。
希尔排序的时间是所取“增量”序列的函数,但目前尚未能求出一种最好的增量序列,是一个未解数据难题,值得注意的是,增量序列中的值没有除1之外的公因子,并且最后一个增量必须等于1.
三、python实现
# coding=utf-8
# @Time : 2021/4/5 12:08
# @Author : SXZXL
# @File : ShellsSort.PY
# @Software : PyCharm
import math
#from Algorithm_Code.InsertSort import Insert_Sort
from numpy import sort
def Insert_Sort(S):
newS = [S[0]]
for i in S[1:]:
temp = list(sort(newS+[i]))
i_index = temp.index(i)
newS.insert(i_index,i)
return newS
def shell_sort(S, a):
# if a < 2:
# S = Insert_Sort(S)
# else:
while a > 1:
for i in range(a):
temp = []
for j in range(i,len(S),a):
temp.append(S[j])
temp = Insert_Sort(temp)
for k in range(i,len(S),a):
S.pop(k)
S.insert(k,temp[0])
temp.pop(0)
a = math.ceil(a/2)
# S = shell_sort(S, a)
return Insert_Sort(S)
# S = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
S = [49, 38, 65, 97, 76, 13, 27, 49, 23, 94, 81, 1, 95, 3, 99]
print(shell_sort(S,5))