Merge and Quik sort

In this homework you will write two sorts over lists that sort in time proportional to n * log(n) where n is the length of the input in the average case.

What to do


-- Author : _________________

module MergeAndQuiksort where

import ArrCommands
import Test.HUnit(Test(TestCase,TestList),assertEqual,runTestTT)

-- This function make a test out of a list of assertions
assertsToTest asserts = TestList [ TestCase a | a <- asserts ]

-------------------------------------------------
-- Invariant split is never called on an empty list
-- Splits a list into 2 lists, roughly equal in length

split :: Ord a => [a] -> ([a],[a])
split xs = undefined

a8 =  assertEqual "splitEven" (split [3,4,5,8,4,5])     ([3,4,5],[8,4,5])
a9 =  assertEqual "splitOdd"  (split [6,2,8,4,3,8,99])  ([6,2,8],[4,3,8,99])
a10 = assertEqual "split1"  (split [4]) ([],[4])

t2 = assertsToTest [a8,a9,a10]


----------------------------------------------------------------
-- Invariant: merge is always applied to 2 already sorted lists
-- merges two sorted lists into a single sorted list

merge:: Ord a => [a] -> [a] -> [a]
merge [] ys = undefined
merge xs [] = undefined
merge (x:xs) (y:ys) | x <= y = undefined
merge (x:xs) (y:ys) | x >  y = undefined

a11 = assertEqual "merge1" (merge [1,4,7,9] [2,5,5,7])  [1,2,4,5,5,7,7,9]
a12 = assertEqual "merge2" (merge [1,4,7,9] [])         [1,4,7,9]

t3 = assertsToTest [a11,a12]


-------------------------------------------------------
-- Sorts a list by doing the following
-- The input [4,2,6,3,77,43,1,99,6]  is broken into two pieces
-- [4,2,6,3] and [77,43,1,99,6]
-- then each is sorted to get
-- [2,3,4,6] and [1,6,43,77,99]
-- then the pieces are combined to get
-- [1,2,3,4,6,6,43,77,99]


mergesort:: Ord a => [a] -> [a]
mergesort [] = undefined
mergesort [x] = undefined
mergesort (x:xs) = undefined

a13 = assertEqual "" (mergesort [4,2,6,3,77,43,1,99,6]) [1,2,3,4,6,6,43,77,99]
a14 = assertEqual "" (mergesort []) ([]::[Int])

t4 = assertsToTest [a13,a14]

mergeTests = TestList [t2,t3,t4]

-- ============================================
-- Quiksort

-------------------------------------------------------------------
-- allMore selects only those elements from "list" greater than "n"

allMore :: Ord a => a -> [a] -> [a]
allMore n list =undefined

a4 = assertEqual "emptyMore" (allMore 3  [])            []
a5 = assertEqual "someMore"  (allMore 9  [11,3,41,9,2]) [11,41]
a6 = assertEqual "noneMore"  (allMore 12 [5,6,7])       []
a7 = assertEqual "allMore"   (allMore 2  [5,6,7])       [5,6,7]

----------------------------------------------------------------------------
-- allLess selects only those elements from "list" less than or equal to "n"

allLess :: Ord a => a -> [a] -> [a]
allLess n list = undefined

a1 = assertEqual "emptyLess" (allLess 4 [])           []
a2 = assertEqual "someLess"  (allLess 4 [1,3,4,5,2])  [1,3,4,2]
a3 = assertEqual "noneLess"  (allLess 4 [5,6,7])      []

t1 = assertsToTest [a1,a2,a3,a4,a5,a6,a7]

-----------------------------------------------------------
-- splitBy takes a pivot value and a list, and breaks the
-- list into lists where the left elements are <= the pivot
-- and the right elements are > the pivot

splitBy:: Ord a => a -> [a] -> ([a],[a])
splitBy x xs = undefined


a15 = assertEqual "split1" (splitBy 3 [4,3,8,4,5])   ([3],[4,8,4,5])
a16 = assertEqual "split2" (splitBy 6 [2,5,4,3,6,2]) ([2,5,4,3,6,2],[])
a17 = assertEqual "split3" (splitBy 4 [6,8,9])       ([],[6,8,9])

t5 = assertsToTest [a15,a16,a17]

-------------------------------------------------------
-- Sorts a list by doing the following
-- The input [4,2,6,3,77,43,1,99,6]  is broken into two pieces
-- [2,3,1] and [6,77,43,99,6]
-- where the elements on the left are all less or equal to the original first element (4)
-- where the elements on the right are all greater then the original first element (4)
-- then each piece is sorted to get
-- [1,2,3] and [6,6,43,77,99]
-- then the pieces, and the original element are combined
-- [1,2,3] and 4 and [6,6,43,77,99] to get
-- [1,2,3,4,6,6,43,77,99]


quiksort:: Ord a => [a] -> [a]
quiksort [] = undefined
quiksort [x] = undefined
quiksort (x:xs) = undefined

a18 = assertEqual "" (quiksort [4,2,6,3,77,43,1,99,6]) [1,2,3,4,6,6,43,77,99]
a19 = assertEqual "" (quiksort []) ([]::[Int])

t6 = assertsToTest [a18,a19]

-------------------------------------------------------

tests = runTestTT  (TestList [t1,t2,t3,t4,t5,t6])

main = undefined


What to turn in.

Create a file by cutting and pasting the template between the dark black horizontal lines. Be sure you do the following

Back to the Daily Record.

Back to the class web-page.