CS 251 Lecture October 20 & October 22
Merge Sort
•
Sorting mechanism that uses
Divide and Conquer
•
Divide and Conquer
is an algorithm strategy that divides a problem into smaller
subproblems
•
Then it solves these subproblems by dividing them and so on until the solution
is trivial
•
Then it puts the solutions back together into the full solution
Steps of
Divide and Conquer
1.
Divide
. Divide problem into smaller subproblems
2.
Recursively solve the subproblems
3.
Conquer
. Put together the solution to subproblems into a bigger solution
Merge Sort
•
Sorting algorithm that uses
Divide and Conquer
•
At each step, it divides the array to be sorted into two sets:
S
1
and
S
2
such that
the sizes of
S
1
and
S
2
differ by a maximum of 1
•
Apply recursively merge sort to
S
1
and
S
2
. Continue to divide until the number
of elements in
S
1
and
S
2
is at most 1
•
At this point, the two sets are sorted because a set containing at most one
element is sorted.
•
Merge sets
S
1
and
S
2
by placing them in the desired sorted order
For example, let
S
= { 3, 8, 15, 23, 6, 1, 10 }
Let red =
S
1
, green =
S
2
:
{ 3, 8, 15, 23, 6, 1, 10 }
Divide
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
{ 3, 8, 15, 23 }
{ 6, 1, 10 }
{ 3, 8 }
{ 15, 23 }
{ 6, 1 }
{ 10 }
{ 3 }
{ 8 }
{ 15 }
{ 23 }
{ 6 }
{ 1 }
{ 10 }
{ }
Conquer
{ 3, 8 }
{ 15, 23 }
{ 1, 6 }
{ 10 }
{ 3, 8, 15, 23 }
{ 1, 6, 10 }
{ 1, 3, 6, 8, 10, 15, 23 }
•
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Staff
 Data Structures, Sort

Click to edit the document details