python3算法合集

1.排序时间空间复杂度

kind speed worst case work space stable
快速排序 1 O(n^2) 0 no
堆排序 3 O(n*log(n)) 0 no
合并排序 2 O(n*log(n)) ~n/2 yes
‘timsort’ 2 O(n*log(n)) ~n/2 yes

快速排序

def quickSort(sourceList):
    if len(sourceList) <= 1:
        return sourceList
    middle = sourceList[len(sourceList)//2]
    leftList = [x for x in sourceList if x < middle]
    rightList = [x for x in sourceList if x > middle]
    return quickSort(leftList) + [middle] + quickSort(rightList)

python3算法合集_第1张图片

你可能感兴趣的:(python,算法)