Haskell描述
--file Sort.hs
--Straight Insertion Sort
--1
isort1::(Ord a)=>[a]->[a]
isort1 []=[]
isort1 (x:xs)=insert x $ isort1 xs
where insert::(Ord a)=>a->[a]->[a]
insert x [] =[x]
insert x (y:ys)
| x<y = x:y:ys
| otherwise = y:insert x ys
--2
insert::(Ord a)=>a->[a]->[a]
insert x [] =[x]
insert x (y:ys)
| x<y = x:y:ys
| otherwise = y:insert x ys
insertSort::(Ord a)=>[a]->[a]->[a]
insertSort xs []=xs --xs是用于存储中间结果的列表
insertSort xs (y:ys)=insertSort (insert y xs) ys
isort2::(Ord a)=>[a]->[a]
isort2=insertSort []
--file Sort.hs
--Bubble Sort
swap::Ord a=>[a]->[a]
swap [] =[]
swap (x:[]) =[x]
swap (x:x':xs)
| x>x' =x': swap (x:xs)
| otherwise =x : swap (x':xs)
bsort::Ord a=>[a]->[a]
bsort xs
| swap xs == xs =xs
| otherwise =bsort (swap xs)
--Quick Sort
qsort::Ord a=>[a]->[a]
qsort []=[]
qsort (x:xs)=qsort smaller ++ x:qsort greater
where smaller =filter (<=x) xs
greater =filter (> x) xs
--file Sort.hs
-- Simple Selection Sort
deletedFrom::Eq a=>a->[a]->[a]
deletedFrom _ []=[]
deletedFrom x (y:ys)
| x==y =ys
| otherwise =y: (x `deletedFrom` ys)
ssort::Ord a=>[a]->[a]
ssort []=[]
ssort xs= min : ssort xs'
where min= minimum xs
xs'= min `deletedFrom` xs