You are on page 1of 6

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 3 4 6 6

2|6

Merge Sort Algorithm using Haskell

Merge Sort Algorithm using Haskell
0.1 Introduction
In this article we will look at implementation of Merge Sort Algorithm using Haskell.

0.2 Merge Sort
Input the algorithm is unsorted array,and output is the sorted array and possibly number of iterations. Let C be the output array of length (N ) Let us assume we have split the list into two parts A and B. which are of length (N=2) The parts A and B are sorted recurively. Now smallest element original list is smallest element in union of A and B. And this element would lie at index 1 either list A or B. Thus A(1) is compared with B(1) and one with minimum value is placed in the output list C(1). Now one of list has reduced by length 1 ,the same processes is carried out recursively till all the required elements of the output array are filled. The operation consists of merge step and sorting step. First we will look at the number of operation required for single merge of two arrays of length (N=2) .The number of comparision required are N for worst case scenario Now we have look at time require to sort the sub arrays A and B. which are of length (N=2)

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

a

= 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

and size of the

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

2

j

k

N

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)

levels the total number of opera-

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

Merge Sort Algorithm using Haskell

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

Merge Sort Algorithm using Haskell

-- 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