Sort the sequence 3, 1, 4, 1, 5, 9, 2, 6, 5 using insertion sort
Read moreTextbook Solutions for Data Structures and Algorithm Analysis in Java
Question
When implementing quicksort, if the array contains lots of duplicates, it maybe 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-waycomparisons, as provided by the compareTo method.a. Give an algorithm that performs a three-way in-place partition of an N-elementsubarray using only N 1 three-way comparisons. If there are d items equalto 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 UNKNOWN LARGE EQUALi jb. Prove that using the algorithm above, sorting an N-element array that containsonly d different values, takes O(dN) time.
Solution
The first step in solving 7 problem number 28 trying to solve the problem we have to refer to the textbook question: When implementing quicksort, if the array contains lots of duplicates, it maybe 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-waycomparisons, as provided by the compareTo method.a. Give an algorithm that performs a three-way in-place partition of an N-elementsubarray using only N 1 three-way comparisons. If there are d items equalto 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 UNKNOWN LARGE EQUALi jb. Prove that using the algorithm above, sorting an N-element array that containsonly d different values, takes O(dN) time.
From the textbook chapter Sorting you will find a few key concepts needed to solve this.
Visible to paid subscribers only
Step 3 of 7)Visible to paid subscribers only
full solution