-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.hs
More file actions
50 lines (40 loc) · 1.26 KB
/
Copy pathsort.hs
File metadata and controls
50 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Sort
( quicksort
, mergesort
, insertionsort
, bubblesort
) where
import Data.List(delete)
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (x : xs) = quicksort smaller ++ [x] ++ quicksort larger
where smaller = [ s | s <- xs, s <= x]
larger = [ l | l <- xs, l > x]
mergesort :: Ord a => [a] -> [a]
mergesort [] = []
mergesort [x] = [x]
mergesort xs = merge (mergesort left) (mergesort right)
where (left, right) = splitAt (length xs `div` 2) xs
merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x : xs) (y : ys)
| x <= y = x : merge xs (y : ys)
| otherwise = y : merge (x : xs) ys
insertionsort :: Ord a => [a] -> [a]
insertionsort [] = []
insertionsort xs = let m = minimum xs in m : insertionsort (delete m xs)
bsort :: Ord a => [a] -> [a]
bsort [] = []
bsort [x] = [x]
bsort l @ (x : y : t)
| isSorted l = l
| otherwise = if x <= y then x : bsort (y : t) else y : bsort (x : t)
isSorted :: Ord a => [a] -> Bool
isSorted xs = and $ zipWith (<=) xs $ tail xs
doWhile :: (a -> Bool) -> (a -> a) -> a -> a
doWhile p f x = let res = f x in if not (p res) then res else doWhile p f res
bubblesort :: Ord a => [a] -> [a]
bubblesort = doWhile notSorted bsort
notSorted :: Ord a => [a] -> Bool
notSorted = (not . isSorted)