You are on page 1of 28

Merge Sort

Merging
The

key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2xm and !(y1y2yn the resulting list is "(#1#2#m$n

%xam&le'

(1 ) * + , - . (2 ) * 1 / 0 . merge((1, (2 ) * 1 + / 0 , - .

Merging (cont.)
X'

11

2+

/2 !'

2/

0/

3esult'

Merging (cont.)
X'

11

2+

/2 !'

2/

0/

3esult'

Merging (cont.)
X'

11

2+

/2 !'

2/

0/

3esult'

Merging (cont.)
X'

11

2+

/2 !'

2/

0/

3esult'

Merging (cont.)
X'

2+

/2 !'

2/

0/

3esult'

11

Merging (cont.)
X'

/2 !'

2/

0/

3esult'

11

2+

Merging (cont.)
X'

/2 !'

0/

3esult'

11

2+

2/

Merging (cont.)
X' !'

0/

3esult'

11

2+

2/

/2

Merging (cont.)
X' !'

3esult'

11

2+

2/

/2

0/

Divide And Conquer


Merging

a two lists of one element each is the same as sorting them4 Merge sort divides u& an unsorted list until the a5ove condition is met and then sorts the divided &arts 5ack together in &airs4 S&ecifically this can 5e done 5y recursively dividing the unsorted list in half, merge sorting the right side then the left side and then merging the right and left 5ack together4

Merge Sort Algorithm


6iven a list ( with a length k' 7f k )) 1 the list is sorted %lse'
8 Merge Sort the left side (1 thru k92 8 Merge Sort the right side (k92$1 thru k 8 Merge the right side with the left side

Merge Sort Example


-: ,: 1/ /, +/ ,: 2 1

Merge Sort Example


--: : ,: 1/ /, +/ ,: ,: 1/ /, +/ ,: 2 2 1 1

Merge Sort Example


--: : ,: 1/ /, +/ ,: ,: 1/ /, +/ ,: 2 2 1 1

--

,: 1/

/, +/

,:

Merge Sort Example


--: : ,: 1/ /, +/ ,: ,: 1/ /, +/ ,: 2 2 1 1

--

,: 1/

/, +/

,:

--

,:

1/

/,

+/

,:

Merge Sort Example


--: : ,: 1/ /, +/ ,: ,: 1/ /, +/ ,: 2 2 1 1

--

,: 1/

/, +/

,:

--

,:

1/

/,

+/

,:

2 2

1 1

Merge Sort Example

-Merge

,:

1/

/,

+/

,:

1 2

2 1

Merge Sort Example

--

1/ ,:

/, +/

,:

-Merge

,:

1/

/,

+/

,:

Merge Sort Example


: 1/ ,: -1 2 +/ /, ,:

--

1/ ,:

/, +/

,:

Merge

Merge Sort Example


1 : 2 : 1/ +/ /, ,: ,: -1 2 +/ /, ,: 1/ ,: --

Merge

Merge Sort Example


1 2 : 1/ +/ /, ,: ,: --

Algorithms
Algorithm for Mergesort
;lgorithm MergeSort (low, high * if(low<high then * mid')=(low $ high 92>? MergeSort(low, mid ? MergeSort(mid$1,high ? Merge(low, mid, high ? . .
;lgorithm Merge(low, mid, high * h')low? i')low? @')mid $ 1? while (( h<)mid and ( @<)high do * if( aAhB <) aA@B then * 5AiB?)aAhB? h')h$1? . else * 5AiB?)aA@B? @)@$1? . i')i$1? . if(hCmid then for k')@ to high do * 5AiB')aAkB? i')i$1? . else for k')h to mid do * 5AiB')aAkB? i')i$1? . for k')low to high do aAkB')5AkB? .

Algorithm for Merge


;lgorithm Merge(low, mid, high * h')low? i')low? @')mid $ 1? while (( h<)mid and ( @<)high do * if( aAhB <) aA@B then * 5AiB?)aAhB? h')h$1? . else * 5AiB?)aA@B? @)@$1? . i')i$1? . if(hCmid then for k')@ to high do * 5AiB')aAkB? i')i$1? . else for k')h to mid do * 5AiB')aAkB? i')i$1? . for k')low to high do aAkB')5AkB? .

Implementing Merge Sort

There are two 5asic ways to im&lement merge sort' 8 7n Dlace' Merging is done with only the in&ut array Dro' 3eEuires only the s&ace needed to hold the array Fon' Takes longer to merge 5ecause if the next element is in the right side then all of the elements must 5e moved down4 8 Gou5le Storage' Merging is done with a tem&orary array of the same si#e as the in&ut array4 Dro' Haster than 7n Dlace since the tem& array holds the resulting array until 5oth left and right sides are merged into the tem& array, then the tem& array is a&&ended over the in&ut array4 Fon' The memory reEuirement is dou5led4

Merge Sort Anal sis


The Gou5le Memory Merge Sort runs I (J log J for all cases, 5ecause of its Givide and FonEuer a&&roach4 T(J ) 2T(J92 $ J ) I(J logJ

!inall "
There are other variants of Merge Sorts including kK way merge sorting, 5ut the common variant is the Gou5le Memory Merge Sort4 Though the running time is I(J logJ and runs much faster than insertion sort and 5u55le sort, merge sortLs large memory demands makes it not very &ractical for main memory sorting4

You might also like