受Haskell的快排啟發(fā)
qsort :: [Int] -> [Int]
qsort [] = []
qsort (pivot:others) = (qsort lowers) ++ [pivot] ++ (qsort highers)
where lowers = filter (<pivot) others
highers = filter (>=pivot) others
嘗試了用python實(shí)現(xiàn):
def qsort(lst):
if not lst:
return []
pivot = lst[0]
sub_lst = lst[1:]
return qsort(lower_elements(pivot, sub_lst))+[pivot]+qsort(highter_elements(pivot, sub_lst))
def lower_elements(pivot,lst):
return [x for x in lst if x < pivot]
def highter_elements(pivot, lst):
return [x for x in lst if x >= pivot]
使用python實(shí)現(xiàn)更容易理解了(:зゝ∠)