码迷,mamicode.com
首页 > 编程语言 > 详细

【陌陌】Python 快排

时间:2015-04-08 19:30:25      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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

 

【陌陌】Python 快排

标签:

原文地址:http://www.cnblogs.com/ztgentle/p/4403360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!