CSC 212: Data Structures and Abstractions University of Rhode Island
CSC 212: Data Structures and Abstractions University of Rhode Island
CSC 212: Data Structures and Abstractions University of Rhode Island
Instructions
You may discuss problems with instructors or other students, however all work submitted must
be your own!
Please include this sheet with your solutions and type them or write extremely neatly. Your work
must be easily legible in order for it to be graded.
Show all of your work and reasoning!
Post to Piazza for clarifications if you have any questions.
The total value of the assignment is 100 points, where each ? represents 5 points and each
represents 10 points.
Sorting
1. ? Clearly show each step of Insertion, Selection, Merge and Quick sorts on the sequence 3, 1, 4, 1, 5, 9, 2, 6, 5.
2. ? What is the running time of heapsort for presorted input?
3. Determine the running time of mergesort for:
(a) sorted input
(b) reverse-ordered input
(c) random input (you may assume a uniform random distribution)
4. ? Suppose we choose the element in the middle position of the array as pivot. Does this make it
unlikely that quicksort will require quadratic time? Why or why not? Explain your reasoning.
5. ? When implementing quicksort, if the array contains lots of duplicates, it may be better to
perform a three-way partition (into elements less than, equal to, and greater than the pivot) to
make smaller recursive calls. Assume three-way comparisons. Give an algorithm that performs a
three-way in-place partition of an N -element subarray using only N 1 three-way comparisons.
If there are d items equal to the pivot, you may use d additional Comparable swaps, above and
beyond the two-way partitioning algorithm. (Hint: As i and j move toward each other, maintain
five groups of elements as shown below):
EQUAL
SMALL
i
UNKNOWN
LARGE
EQUAL