快速排序

2018-09-09  本文已影响0人  地铁姑娘

算法分析

代码实现

def quickSort(Arr):
    if len(Arr)<2:
        return Arr
    else:
        base = Arr[0]
        left = []
        right = []
        num = len(Arr)
        for i in range(1,num):
            if base<Arr[i]:
                left.append(Arr[i])
            else:
                right.append(Arr[i])
    L = quickSort(left)
    R = quickSort(right)
    return [L,base,R]
result = []
def print_list(n):
    for i in n:
        if isinstance(i,list):
            print_list(i)
        else:
            result.append(i)
    return result
if __name__ == '__main__':
    arrayAim = []
    for i in range (10):
        a = random.randint (1, 10)
        arrayAim.append (a)
    ourResult = quickSort(arrayAim)
    print print_list(ourResult)
上一篇下一篇

猜你喜欢

热点阅读