📚 The CoCalc Library - books, templates and other resources
License: OTHER
module Sort where12-- Aufgabe 23-- insert list el: sorts an elmenet el into a sorted list4insert :: (Ord t) => [t] -> t -> [t]5insert [] x = [x]6insert [a] x7| x < a = [x, a]8| otherwise = [a, x]9insert (a:b:qs) x10| x < a = [x,a,b] ++ qs11| x < b = [a,x,b] ++ qs12| otherwise = [a,b] ++ insert qs x1314-- sortH q r: Sorts an unsorted list r into a sorted list q15insertH :: (Ord t) => [t] -> [t] -> [t]16insertH q [] = q17insertH q [r] = insert q r18insertH q (r:rest) = insertH (insert q r) rest1920-- insertSort list: sorts list21insertSort :: (Ord t) => [t] -> [t]22insertSort [] = []23insertSort [a] = [a]24insertSort (a:qs) = insertH [a] qs2526-- Aufgabe 327merge :: (Ord t) => [t] -> [t] -> [t]28merge [] x = x29merge x [] = x30merge (x:xs) (y:ys)31| x <= y = x : merge xs (y:ys)32| otherwise = y : merge ys (x:xs)3334mergeSort :: (Ord t) => [t] -> [t]35mergeSort [] = []36mergeSort [x] = [x]37mergeSort xs = merge (mergeSort top) (mergeSort bottom) where38(top, bottom) = splitAt (div (length xs) 2) xs3940-- Aufgabe 441-- Teste42isSorted :: (Ord t) => [t] -> Bool43isSorted [] = True44isSorted [a] = True45isSorted (a:b:xs)46| (a <= b) && isSorted xs = True47| otherwise = False4849insertSortedIsSorted :: (Ord t) => [t] -> Bool50insertSortedIsSorted xs = isSorted(insertSort xs)5152mergeSortedIsSorted :: (Ord t) => [t] -> Bool53mergeSortedIsSorted xs = isSorted(mergeSort xs)545556