【陌陌】Python 快排

import random

def qsort2(array):
    if len(array) <= 1: return array
    else: return qsort2([i for i in array[1:] if i<array[0]]) + [array[0]] + qsort2([i for i in array[1:] if i>=array[0]])

def qsort(array, left, right):
    if left >= right: pass
    else:
        print array,left,right
        low = left+1
        high = right
        index = left
        while low <= high:
            while low<=high and array[low]<array[index]:
                low += 1
            while low<=high and array[high]>=array[index]:
                high -= 1
            if low<high:
                array[low],array[high] = array[high],array[low]
        if high > index:
            array[index],array[high]=array[high],array[index]
        print array, high
        print 
        qsort(array, left, high-1)
        qsort(array, high+1, right)

x = [random.randint(0,100) for i in range(10)]
print x
qsort(x,0,9)
print x

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。