home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-06-01 | 1.5 KB | 14 lines | [TEXT/GFER] |
- {- list sorting: see L.C.Paulson, ML for the working programmer, Cambidge, p100
- -- The list is split into ascending chunks which are then merged in pairs.
-
- samsort l = sorting [] 0 l
- where sorting ls k [] = head(mergepairs ls 0)
- sorting ls k (x:xs) = sorting (mergepairs (run:ls) kinc) kinc tl
- where (run, tl) = nextrun [x] xs
- kinc = k+1
- nextrun run [] = (reverse run, [])
- nextrun rs@(r:_) xl@(x:xs)
- | x<r = (reverse rs, xl)
- | otherwise = nextrun (x:rs) xs
- mergepairs [l] _ = [l]
- mergepairs l