Python实现快速排序算法

import random
import copy

def quickSort(a):
    def swap(i,j):
        tmp = a[i]
        a[i] = a[j]
        a[j] = tmp       
    def partition(start,end):
        if start >= end:
            pass
        elif (start+1) == end:
            if a[start] > a[end]:
                swap(start,end)
        else:
            i = start + 1
            j = end
            while i < j:
                while (a[i] <= a[start]) and i= a[start] and j >start):
                    j = j -1
                if i < j:
                    swap(i,j)                   
            if j != start:
                swap(j,start)
            partition(start,j-1)
            partition(j+1,end)
    partition(0,len(a)-1)
    

def genList(n):
    result = []
    for i in range(n):
        result.append(random.randint(0,100))
    return result
alist = genList(200)
blist = copy.deepcopy(alist)
blist.sort()
#alist = list(range(20))
#alist = list(range(20,-1,-1))
quickSort(alist)
print((alist == blist))
print(alist)
print(blist)

 

你可能感兴趣的:(python,算法,数据结构)