Page 1
initiallyTreat data as N sorted collections that are each one datum long. mergeMerge each consecutive pair of collections to form sorted collections twice as large
repeatrepeat the merge step over and over until a single sorted collection is formed.
A more efficient sorting algorithm results from repeatedly merging small sorted lists to make larger sorted lists.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 2
initially Treat data as N sorted collections that are each one datum long.
1st merge
Example (of merge sort)
5
5 8 6 2 9 4 3 0
8 6 2 9 4 3 0
5 8 2 6 4 9 0 3
2nd merge
2 5 6 8 0 3 4 9
3rd merge
0 2 3 4 5 6 8 9
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 3
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 4
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 5
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 6
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 7
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 8
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 9
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 10
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 11
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 12
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 13
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4 5
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 14
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4 5
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 15
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4 5 6
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 16
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4 5 6
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 17
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4 5 6 8
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 18
What is the most efficient way to merge two sorted lists to make a larger sorted list?
Example
2 5 6 8 0 3 4 9
0 2 3 4 5 6 8 9
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 19
Number of merges5 8 6 2 9 4 3 0
5 8 2 6 4 9 0 3
2 5 6 8 0 3 4 9
0 2 3 4 5 6 8 9
2 5 6 8 0 3 4 9
0 2 3 4 5 6 8 9
Number of Probes per merge
AVERAGE Number of Probes per merge
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 20
Partitioning is done with respect to a pivot, which is one value of the collection being partitioned.
Another efficient sorting algorithm results from recursively partitioning collections into two smaller groups until every group is of size one (singleton groups), then concatenating all of the singleton groups.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 21
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 22
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 23
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot left == 2
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 24
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot left == 2
0 4 3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 25
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot right == 8
2nd pivot left == 2
0 4 3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 26
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot right == 8
6 9
2nd pivot left == 2
0 4 3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 27
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot right == 8
6 9
2nd pivot left == 2
3rd pivot == 4
0 4 3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 28
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot right == 8
6 9
2nd pivot left == 2
3rd pivot == 4
0 4 3
3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 29
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
2 4 3 0 8 6 9
2nd pivot right == 8
6 9
concatenate
2nd pivot left == 2
3rd pivot == 4
0 4 3
3
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 30
2 4 3 0 8 6 9
2nd pivot right == 8
6 9
concatenate
0 2 3 4 5 6 8 9
2nd pivot left == 2
3rd pivot == 4
0 4 3
3
initially Select one of the values as a pivot and partition into groups smaller and larger than pivot1st pivot == 5
Example (of quicksort)
5 8 6 2 9 4 3 0
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 31
public Group quickSort(Group g) { Comparable pivot; Group small, large;
if (sizeOf(g) <= 1) { return g; } else { pivot = selectedPivot(g); //partition g into small and large return concatenate( quickSort(small), pivot, quickSort(large)); }}
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Page 32
Quicksort is more difficult to anlyze.
However, in practice ...
Worst Case?
Best Case?
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.