Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active May 29, 2020 18:50
Show Gist options
  • Save Superstar64/c9a185778e976d12ca70968a0ba46efc to your computer and use it in GitHub Desktop.
Save Superstar64/c9a185778e976d12ca70968a0ba46efc to your computer and use it in GitHub Desktop.
Merge Insertion(Ford-Johnson) Sort in Haskell
{-
Copyright(C) Freddy A Cubas "Superstar64"
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
-}
import Data.List
import Data.Foldable
import Data.Traversable
import qualified Data.Bifunctor as Bifunctor
pairUp :: [a] -> ([(a,a)],Maybe a)
pairUp [] = ([],Nothing)
pairUp [a] = ([],Just a)
pairUp (a:b:t) = Bifunctor.first ((a,b) :) $ pairUp t
sortPair :: Monad m => (a -> a -> m Bool) -> (a,a) -> m (a,a)
sortPair compare (left,right) = do
less <- compare left right
if less then
return (left,right)
else
return (right,left)
indexes :: (Ord a, Enum a, Num a) => a -> [(a, a)]
indexes size = distribute differences 0 where
differences = map snd $ iterate (\(i,a) -> (i+1,2^(i + 2) - a)) (0,2)
distribute ~(box:_) current | current + box >= size = []
distribute ~(box:rest) current = reverse [(current + box + 2, x) | x <- [current .. current + box - 1]] ++ distribute rest (current + box)
binaryInsert :: Monad m => (a -> a -> m Bool) -> Int -> Int -> a -> [a] -> m [a]
binaryInsert compare start end item list | start == end = return $ initial ++ [item] ++ final where
(initial,final) = splitAt start list
binaryInsert compare start end item list = do
let center = (start + end) `quot` 2
less <- compare item (list !! center)
if less then
binaryInsert compare start center item list
else
binaryInsert compare (center + 1) end item list
-- https://en.wikipedia.org/wiki/Merge-insertion_sort
mergeInsertSort :: Monad m => (a -> a -> m Bool) -> [a] -> m [a]
mergeInsertSort _ [] = return []
mergeInsertSort _ [a] = return [a]
mergeInsertSort compare list = do
let (paired,final) = pairUp list
paired' <- traverse (sortPair compare) paired
~((first,second):paired'') <- mergeInsertSort (\(_,left) (_,right) -> compare left right) paired'
let sorted = first : second : map snd paired''
let unsorted = map fst paired''
let order = indexes (length unsorted)
let unsorted' = maybe unsorted (\final -> unsorted ++ [final]) final
let order' = order ++ [ (length sorted + i,i) | i <- [length order .. length unsorted' - 1]]
let queue = (map . Bifunctor.second) (unsorted' !!) order'
foldlM (flip $ uncurry $ binaryInsert compare 0) sorted queue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment