Sorting (Part II: Divide and Conquer) : CSE 373 Data Structures
Sorting (Part II: Divide and Conquer) : CSE 373 Data Structures
Conquer)
CSE 373
Data Structures
Lecture 14
Readings
• Reading
› Section 7.6, Mergesort
› Section 7.7, Quicksort
8 2 9 4 5 3 1 6
2 4 8 9 1 3 5 6
Auxiliary array
2 4 8 9 1 3 5 6
Auxiliary array
1
2 4 8 9 1 3 5 6
Auxiliary array
1 2 3 4 5
i j normal
target
Left completed
copy i j
first
target
target
Merge by 1
Merge by 2
Merge by 4
Merge by 8
Merge by 1
Merge by 2
Merge by 4
Merge by 8
Merge by 16
S1 0
S2 partition S
43 31 75
13 65
92 81
26 57
QuickSort(S1) and
S1 S2 QuickSort(S2)
0 13 26 31 43 57 65 75 81 92
S 0 13 26 31 43 57 65 75 81 92 Voila! S is sorted
[Weiss]
8 1 4 9 0 3 5 2 7 6
Median of 0, 6, 8 is 6. Pivot is 6
0 1 4 9 7 3 5 2 6 8
i j
Place the largest at the right
and the smallest at the left.
Swap pivot with next to last element.
Example
i j
0 1 4 9 7 3 5 2 6 8
i j
0 1 4 9 7 3 5 2 6 8
i j
0 1 4 9 7 3 5 2 6 8
i j
0 1 4 2 7 3 5 9 6 8
0 1 4 2 7 3 5 9 6 8
i j
0 1 4 2 7 3 5 9 6 8
i j
0 1 4 2 5 3 7 9 6 8
ij
0 1 4 2 5 3 7 9 6 8
j i
0 1 4 2 5 3 7 9 6 8 Cross-over i > j
j i
0 1 4 2 5 3 6 9 7 8