首页 > 代码库 > Python快速排序

Python快速排序

快排,取一个key值,一般取第一个即可,将小于key的放到左边,大于key的放到右边,递归实现

 1 import random 2 def quicksort(data, low = 0, high = None): 3     if high == None: 4         high = len(data) - 1 5     if low < high: 6         s, i, j = data[low], low, high 7         while i < j: 8             while i < j and data[j] >= s: 9                 j = j - 110             if i < j:11                 data[i] = data[j]12                 i = i + 113             while i < j and data[i] <= s:14                 i = i + 115             if i < j:16                 data[j] = data[i]17                 j = j - 118         data[i] = s19         quicksort(data, low, i - 1)20         quicksort(data, i + 1, high)21 22 data=http://www.mamicode.com/[random.randint(1,100) for i in range(100)]23 print(data)24 quicksort(data)25 print(data)
View Code

 

Python快速排序