首页 > 代码库 > Scheme 快速排序
Scheme 快速排序
#!r6rs
( import ( rnrs base ( 6 ) ) )
( define ( filter pred items )
( cond [ ( null? items ) ‘() ]
[ ( pred ( car items ) )
( cons ( car items )
( filter pred ( cdr items ) ) ) ]
[ else ( filter pred ( cdr items ) ) ] ) )
( define ( quicksort items )
( cond [ ( null? items ) ‘() ]
[ else ( let* ( [ cut ( car items ) ]
[ rest ( cdr items ) ]
[ left ( filter ( lambda ( elem )
( < elem cut ) ) rest ) ]
[ right ( filter ( lambda ( elem )
( >= elem cut ) ) rest ) ] )
( append ( quicksort left )
( list cut )
( quicksort right ) ) ) ] ) )
( define LL ‘( 1 3 44 2 6 7 88 9 10 -1 ) )
( quicksort LL ) => {-1 1 2 3 6 7 9 10 44 88}