Python 快排[pythonnic]

def QS(array):
    less = []
    more = []
    
    if len(array) <= 1:
        return array
    
    head = array.pop()
    
    for x in array:
        if x <= head:
            less.append(x)
        else:
            more.append(x)
    
    return QS(less) + [head] + QS(more)
    
if __name__ == '__main__':
    array = [8,10,9,6,4,16,5,13,26,18,2,45,34,23,1,7,3]
    print(array)
    print(QS(array))

 

你可能感兴趣的