CSE 331 Spring 2008
Homework 3 Solutions
1.
The following diagram shows how quicksort would operate on this list.
All lists are given after
the partitioning step.
Pivots are shown in
boldface
.
Only the sublist that quicksort actually
works on is shown for each call.
In this diagram, it assumed that quicksort works as given in the
text, only modified so as not to call insertion sort.
median3() swaps elements to find the pivot.
For the following diagram, median3() swaps the pivot to the far right of the list.
No other
swapping is done during the partitioning step.
This is different from the algorithm in the text.
quicksort(A, 0, 0)
233, 132, 327,
414
, 877, 542, 433, 986, 554
132,
233
, 327
132
327
433, 542,
554
, 986, 877
433
, 542
542
877,
986
877
quicksort(A, 0, 2)
quicksort(A, 2, 2)
quicksort(A, 4, 5)
quicksort(A, 4, 8)
quicksort(A, 7, 8)
quicksort(A, 5, 5)
quicksort(A, 7, 7)
233, 132, 327,
414
, 986, 542, 433, 554, 877
132,
233
, 327
132
327
433, 542, 554,
877
, 986
433,
542
, 554
554
986
quicksort(A, 0, 2)
quicksort(A, 2, 2)
quicksort(A, 4, 6)
quicksort(A, 4, 8)
quicksort(A, 8, 8)
quicksort(A, 4, 4)
433
quicksort(A, 6, 6)
quicksort(A, 0, 0)