Presentation is loading. Please wait.

Presentation is loading. Please wait.

Merge Sort Overview.

Similar presentations


Presentation on theme: "Merge Sort Overview."— Presentation transcript:

1 Merge Sort Overview

2 Merge Sort Basic idea Divide data into two (smaller) parts, recursively Sort the parts Merge the sorted parts 3 steps: divide, recurse, merge It was invented by John von Neumann in 1945.

3 Mergesort Note: End cases missing in the pseudocode MergeSort(A):
L <- mergeSort(A[1:n/2]) R <- mergeSort(A[n/2 + 1 : n]) return(merge(L,R)) merge(L,R): S <- Empty array of size m // m = length of L + length of R i <- 1 j <- 1 for k = 1 to m if L[i] < R[j]: S[k] <- L[i] i <- i + 1 else: S[k] <- R[j] j <- j + 1 return(S) Note: End cases missing in the pseudocode

4 Merge Sort - Divide {7,3,2,9,1,6,4,5} {7,3,2,9} {1,6,4,5} {7,3} {2,9}
{1,6} {4,5} Give out cards, order suits: hearts, diamonds, spades, clubs Ask them to sort this way. Point out physical ease. {7} {3} {2} {9} {1} {6} {4} {5}

5 Merge Sort - Merge {1,2,3,4,5,6,7,9} {2,3,7,9} {1,4,5,6} {3,7} {2,9}
{1,6} {4,5} {7} {3} {2} {9} {1} {6} {4} {5}

6 Merge Sort - Divide n n / 2 n / 4 lg n . 1 n
Give out cards, order suits: hearts, diamonds, spades, clubs Ask them to sort this way. Point out physical ease. 1 n


Download ppt "Merge Sort Overview."

Similar presentations


Ads by Google