Professional Documents
Culture Documents
Lecture 5-Merge Sort
Lecture 5-Merge Sort
Introduction
⚫ Many useful algorithms are recursive in structure: to
solve a given problem, they call themselves recursively
one or more times to deal with closely related
subproblems.
⚫ These algorithms typically follow a
divide-and-conquer approach: they break the
problem into several subproblems that are similar to
the original problem but smaller in size, solve the
subproblems recursively, and then combine these
solutions to create a solution to the original problem.
Divide and conquer approach
Mergesort
⚫Mergesort (divide-and-conquer)
⚫ Divide array into two halves.
A L G O R I T H M S
A L G O R I T H M S divide
Mergesort
⚫Mergesort (divide-and-conquer)
⚫ Divide array into two halves.
⚫ Recursively sort each half.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
Mergesort
⚫Mergesort (divide-and-conquer)
⚫ Divide array into two halves.
⚫ Recursively sort each half.
⚫ Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
A G H I L M O R S T merge
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done. first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
Merging
⚫Merge.
⚫ Keep track of smallest element in each sorted half.
⚫ Insert smallest of two elements into auxiliary array.
⚫ Repeat until done. first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S T auxiliary array
Divide and Conquer
⚫ Divide the problem into a number of subproblems
that are smaller instances of the
same problem.
⚫ Conquer the subproblems by solving them
recursively. If the subproblem sizes are small enough,
however, just solve the subproblems in a
straightforward manner.
⚫ Combine the solutions to the subproblems into the
solution for the original problem.
Merge sort
⚫ Divide: Divide the n-element sequence to be
sorted into two subsequences of n/2 elements
each.
cn
cn
Cost of divide
and merge.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4 T(n/4 T(n/4 T(n/4
Cost of sorting ) ) ) )
subproblems.
Comp 122
Running time at each level of
merge –recursive merge sort calls
⚫ 2 x n/2 +4xn/4+8 xn/8…
⚫ 2 units of time and n/2 merge sort calls
⚫ Then..
⚫ 4 units of time and n/4 such merge sort calls
⚫ 8 units of time and n/8…
⚫ As the subproblems get smaller, the number of
subproblems doubles at each "level" of the recursion,
but the merging time halves.
⚫ The doubling and halving cancel each other out, and
so the total merging time is cn at each level of
recursion.
⚫ Eventually, we get down to subproblems of size 1
⚫ We have to spend Θ(1) time to sort subarrays of size 1,
because we have to test whether p <r, and this test
takes time.
⚫ How many subarrays of size 1 are there?
⚫ Since we started with n elements, there must be n of
them.
⚫ Since each base case takes Θ(1) time, let's say that
altogether, the base cases take cn time:
How many levels ?
⚫ he total time for mergeSort is the sum of the merging
times for all the levels.
⚫ If there are l levels in the tree, then the total merging
time is l.cn
⚫ So what is l ?
⚫ We start with subproblems of size n and repeatedly
halve until we get down to subproblems of size 1.
⚫ The answer is l = lg n + 1
⚫ For example, if n=8, then lg n + 1 = 4, and sure
enough, the tree has four levels:
⚫ n = 8, 4, 2, 1
⚫ . The total time for mergeSort, then, is cn.(lg n +1)
Running time
⚫ When we use big-Θ notation to describe this running
time, we can discard the low-order term 1
⚫ and the constant coefficient c, giving us a running
time Θ(n lg n)