| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
hard Solved by 2 students
QuickSort Partition With Duplicate Elements
< Prev
Next >

What is a potential drawback of the partition implementation shown below when the input array contains a large number of duplicate elements?

public class QuickSortEqualPivot {
    public static int partition(int[] arr, int low, int high) {
        int pivot = arr[high];
        int i = low;
        for (int j = low; j < high; j++) {
            if (arr[j] <= pivot) {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
            }
        }
        int temp = arr[i];
        arr[i] = arr[high];
        arr[high] = temp;
        return i;
    }
}
A

It swaps the pivot element multiple times unnecessarily, making the sort unstable.

B

It can lead to unbalanced partitions, degrading QuickSort’s performance.

C

It produces an incorrect sorted order because duplicates are not correctly compared.

D

It will cause an ArrayIndexOutOfBoundsException due to duplicate values.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1lightingstrikes134222 0m 00s 200
#2znasibli101 0m 00s -10
#3mavericklin12502 0m 00s -20
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy