快排的详细介绍见,简单的说就是取输入序列中的首元素m,然后将除首元素m以外的其它元素分成两组,小于等于m的一组和大于m的一组.将3组元素组合成输入队列:小于等于m + m + 大于m.
下面看一个用haskell实现的快速排序代码:
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted
haskell中有列表解析(List Comprehension)的支持,所以smallerSorted
和biggerSorted
两个集合通过列表解析很方便的就生成了.而Scheme中没有列表解析,所以首先要实现一个类似列表解析的功能将附后条件的列表元素筛选出来:
(define my-filter
(lambda (f l)
(define iter
(lambda (l a)
(if (not (pair? l)) a
(let* ([h (car l)] [r (if (f h) (append a (list h)) a)])
(iter (cdr l) r)))))
(iter l '())))
filter的功能是输入一个条件判断函数f和一个列表l,filter将l中所有满足f的元素组成一个列表并返回.上面代码使用的是accumulator
模式,也就是在迭代模式,通过传进一个a参数在每次递归调用中保存结果.可以注意到这个filter的定义是满足尾递归的.
下面是filter的递归版本:
(define my-filter
(lambda (f l)
(if (not (pair? l)) '()
(let ([h (car l)])
(if (f h) (cons h (filter f (cdr l)))
(filter f (cdr l)))))))
通过cps变换将上述函数转换成尾递归的:
(define my-filter
(lambda (f l)
(define cps
(lambda (l k)
(if (not (pair? l)) (k '())
(let ([h (car l)])
(if (f h) (cps (cdr l) (lambda (x) (k (cons h x))))
(cps (cdr l) (lambda (x) (k x))))))))
(cps l (lambda (x) x))))
有了filter之后我们就可以实现qsort了:
(define qsort
(lambda (l)
(if (not (pair? l)) '()
(let* ([m (car l)]
[large (my-filter (lambda (x) (if (> x m) #t #f)) (cdr l))]
[less (my-filter (lambda (x) (if (<= x m) #t #f)) (cdr l))])
(append (qsort less) (cons m (qsort large)))))))
>(qsort `(5 1 4 2 3 3 7)) -> (1 2 3 3 4 5 7)
比较下与haskell版的区别,首先是没有列表解析,其次是没有模式匹配,需要用if表达式处理.
当然haskell中也是由filter的,下面就是haskell快排的filter版本:
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort (filter (<=x) xs)
biggerSorted = quicksort (filter (>x) xs)
in smallerSorted ++ [x] ++ biggerSorted
在引haskell中fold后代码可以更加简洁和高效:
(define (foldl f init xs)
(define (iter xs acc)
(if (null? xs) acc
(iter (cdr xs) (f acc (car xs)))))
(iter xs init))
(define (foldr f init xs)
(define (iter xs acc)
(if (null? xs) acc
(iter (cdr xs) (f (car xs) acc))))
(iter (reverse xs) init))
(define (qsort l greater)
(if (not (pair? l)) '()
(let ([m (car l)]
[partition (foldr (lambda (x acc)
(let ([small (car acc)]
[large (cadr acc)])
(if (greater x m) (list small (cons x large))
(list (cons x small) large))))
'(()()) (cdr l))])
(append (qsort (car partition) greater)
(cons m (qsort (cadr partition) greater))))))
冒泡排序的详细介绍见
首先要定义一个交换函数:
;交换列表中的两个元素
(define (swap xs n1 n2)
(let ([a (element-at xs n1)]
[b (element-at xs n2)])
(reverse (car (cdr (foldl (lambda (acc x)
(let ([fst (car acc)]
[snd (car (cdr acc))])
(cond [(= fst n1) (list (+ fst 1) (cons b snd))]
[(= fst n2) (list (+ fst 1) (cons a snd))]
[else (list (+ fst 1) (cons x snd))]))) '(1 ()) xs))))))
以下是冒泡排序的实现:
(define (bubble xs)
(define (bubble-imp xs less)
(if (= (length xs) 1) (list (car xs) less);返回最大值和剩余值组成的列表
(let ([f (car xs)]
[s (cadr xs)])
(if (> f s) (bubble-imp (cdr (swap xs 1 2)) (reverse (cons s less)))
(bubble-imp (cdr xs) (reverse (cons f less)))))))
(define (iter xs result)
(if (null? xs) result
(let ([r (bubble-imp xs '())])
(iter (cadr r) (cons (car r) result)))))
(iter xs '()))
辅助过程bubble-imp
用于筛选列表,它的返回值是输入列表的(最大元素 其余剩余元素的列表)bubble-imp
每轮执行都会比较列表的第一和第二个元素,如果第一个元素大于第二个元素则交换它们两的位置,
然后将较小的元素插入less中,之后丢弃表头(较小的元素)用剩余元素执行第二轮迭代,直到输入列表中只剩下1个元素,此时,那个剩余元素就是初始
输入列表中的最大元素,而less中则存放了其余的元素.
也就是每次执行bubble-imp
都会生成一个最大值和其余元素的列表,为了完成排序需要将最大值插入结果列表,然后继续从剩余元素中找出次大值.
'iter'过程完成的就是这个任务,执行bubble-imp
将最大值插入结果列表然后判断是否还有剩余元素,如果有则继续上述过程.
相应的haskell实现
-- 冒泡排序
bubble :: (Ord a) => [a] -> [a]
bubble [] = error "Can't call pass on an empty list, dummy!"
bubble (x:[]) = [x]
bubble xs = iter xs []
where
pass xs left
| size == 2 = if first < second then (first:left,second)
else (second:left,first)
| size > 2 = let remain = tail (tail xs) in
if first < second then pass (second:remain) (first:left)
else pass (first:remain) (second:left)
| size == 1 = ([],first)
where
size = length xs
first = head xs
second = head (tail xs)
iter xs result =
let
passret = (pass xs [])
left = fst passret
max = snd passret
in
if length left == 0 then (max:result)
else iter left (max:result)