When implementing quicksort, if the array contains lots of duplicates, it maybe better | StudySoup

Textbook Solutions for Data Structures and Algorithm Analysis in Java

Chapter 7 Problem 7.28

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

Step 1 of 4)

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.

Step 2 of 7)

Visible to paid subscribers only

Step 3 of 7)

Visible to paid subscribers only

Subscribe to view the
full solution

Title Data Structures and Algorithm Analysis in Java 3 
Author Mark A. Weiss
ISBN 9780132576277

When implementing quicksort, if the array contains lots of duplicates, it maybe better

Chapter 7 textbook questions

×

Login

Organize all study tools for free

Or continue with
×

Register

Sign up for access to all content on our site!

Or continue with

Or login if you already have an account

×

Reset password

If you have an active account we’ll send you an e-mail for password recovery

Or login if you have your password back