希尔排序Python

"""
希尔排序,以插入排序为基础,对无序表进行间隔划分,分成多个子列表,分别进行插入排序。
时间复杂度:O(n)-O(n^2)之间
如果将间隔保持在 2^n-1 (1,3,5,7...),那么时间复杂度会在O[n^(2/3)]左右
"""


def shellSort(alist):
    sublistCount = len(alist) // 2
    while sublistCount > 0:
        for startPosition in range(sublistCount):
            gapInsertSort(alist, startPosition, sublistCount)
        print('After increments of size', sublistCount, 'the list is', alist)
        sublistCount //= 2


def gapInsertSort(alist, start, gap):
    for index in range(start + gap, len(alist), gap):
        currentValue = alist[index]
        position = index

        while position >= gap and alist[position - gap] > currentValue:
            alist[position] = alist[position - gap]
            position -= gap

        alist[position] = currentValue


a = [123, 36, 1, 96, 23, 65, 12, 7, 3, 63, 6]

shellSort(a)
print(a)

你可能感兴趣的:(python,希尔排序,python,shell)