Merge Sort Algorithm Using Haskell
Merge Sort Algorithm Using Haskell
Pi19404
July 29, 2013
Contents
Contents
Merge Sort Algorithm using Haskell
0.1 Introduction . . . . . . 0.2 Merge Sort . . . . . . . 0.2.1 Implementation 0.2.2 Implementation References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 3 4 6 6
2|6
3|6
Merge Sort Algorithm using Haskell let the time required for this be denoted as
T (N ) T (N=2)
= 2T (N=2) + O (N ) 2; d = 1
using the master method we get a = 2; b = gives a worst case run time of O(N log2 N ).
and
= bd
which
Since at each iteration we have 2 smaller lists to operate upon, the number of iterations is log2 (N ) + 1 if N is current number of elements in the list. The number of sub problems at level subproblem is N=2j .
j
is
2j
The total number of operation at level is j ,At each level j the merge operation requires about O(N=2j ) comparison operations .Let it be writtes as k (N=2j )
T
2j
kN
Thus total number of operations at each level j is indepent of j. as there is no j term .This is due to fact that at each recursion tree the number of sub problem increases by 2 ,but the size of sub problems also decreasese by 2 Thus for each of (log2 N tions required as kN .
+ 1)
Thus the total number of operations merge sort executes is at most kN log2 (N ) + kN This algorithm has mush lower run time than that of bubble,insertion or selection sort O(N 2 ) as N is large.
0.2.1 Implementation
The implementation of algorithm in Haskel is provided. The algorithm takes as input a list to be sorted. The output is the number of computations and sorted list. Functions have been defined to perform the split and merge operations.
4|6
The merge function accepts 2 input sorted lists and outputs a combined sorted lists. The Split function accepts a input list and returns a list of size (N=2). The merge_sort algorithms base case is when the length of the input is 1 in which case it returns back the list as it is. For all other cases recusive function is called. First split operation is perform to obtain 2 lists and merge_sort function is recursively called on these two lists merge operation is performed on the output sorted lists and then returned to the parent recursive function.
--split -- split an array of size N into two arrays of size N/2 -- the computation of total length of the list is required split :: Ord a =>[a]->Int->([a],[a],Int) split s i= (l1,l2,(i+1)) where (l1,l2)=splitAt ( div (length(s)) 2 ) s --merge -- the function to merge the 2 sorted lists -- iput is the lists to be sorted and current number of computation -- output is merger list + total number of computation required for merge merge1 :: Ord a =>[a]->[a]->Int->([a],Int) merge1 [] xs i = (xs,i+1) merge1 xs [] i = (xs,i+1) merge1 (x:xs) (y:ys) i |x<y = (x:a1,a2) |otherwise = (y:b1,b2) where (a1,a2)=merge1 xs (y:ys) (i+1) (b1,b2)=merge1 (x:xs) ys (i+1) -- the recrsive for merge sort algorithm -- input is list ,output is merge sorted list and number of computation merge_sort' :: Ord a=>[a]->Int->([a],Int) merge_sort' list i -- base case | length(list)==1=(list,i)
5|6
-- output or merging operation | otherwise =(o1,o2) where -- splitting the list into 2 lists of size N/2 (list1,list2,m5)=(split list 0) -- recursive sorting function called on list of size N/2 (list3,m3)=merge_sort' list1 0 (list4,m4)=merge_sort' list2 0 -- merging sorted functions (o1,o2)=merge1 list3 list4 (m3+m4+m5) -- main function to be called for merge sort algoritm merge_sort :: Ord a =>[a]->([a],Int) merge_sort s = merge_sort' s 0
0.2.2 Implementation
Ill be implementing all the algorithms using haskel. Since recursion is basic mechanism to loop in Haskell ,it fits in naturally within recursive algorithmic structure of divide and conquer algorithms. The code can be found at https://github.com/pi19404/m19404/blob/ master/Algorithm/sort/sort.hs
6|6