Download
puts :: String -> IO ()
puts [] = putChar '\n'
puts (x:xs) = do putChar x
puts xs
frontOfArray :: [Int] -> Int -> [Int]
frontOfArray array i = take i array
endOfArray :: [Int] -> Int -> [Int]
endOfArray array j = drop (j+1) array
middleOfArray :: [Int] -> Int -> Int -> [Int]
middleOfArray array i j = endOfArray (frontOfArray array j) i
swap :: [Int] -> Int -> Int -> [Int]
swap array i j = (frontOfArray array i) ++
[array!!j] ++
(middleOfArray array i j) ++
[array!!i] ++
(endOfArray array j)
swapNeeded :: [Int] -> Int -> Int -> Bool
swapNeeded array i j = array!!i > array!!j
swapIfNeeded :: [Int] -> Int -> Int -> [Int]
swapIfNeeded array i j = if (swapNeeded array i j) then
(swap array i j) else
array
gapCalc :: Int -> Int
gapCalc gap = floor(fromIntegral(gap*10)/13)
newGap :: Int -> Int
newGap gap = if (gapCalc(gap) < 1) then
1 else
gapCalc gap
sortWithGap :: Int -> [Int] -> Int -> [Int]
sortWithGap index array gap =
if (index == length(array)-gap) then
array
else
sortWithGap (index+1) (swapIfNeeded array index (index+gap)) gap
detectEndOfSort :: [Int] -> [Int] -> Int -> [Int]
detectEndOfSort oldarray newarray gap =
if (oldarray == newarray) then
newarray else
startNewGap newarray (newGap gap)
startNewGap :: [Int] -> Int -> [Int]
startNewGap array gap = detectEndOfSort array (sortWithGap 0 array gap) gap
combsort :: [Int] -> [Int]
combsort array = startNewGap array (newGap (length array))
dataSource :: [Int]
dataSource = [1,6,5,3,8,6,9,7,2,4,0]
main :: IO ()
main = do puts(show dataSource)
puts(show(combsort dataSource))