| preferred AP College board partner for AP classes
hard
QuickSort Partition Condition
< Prev
Next >

In the partition method for QuickSort provided below, what potential issue arises if the condition in the if statement is changed from arr[j] < pivot to arr[j] <= pivot?

public class SortUtil {
    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 would optimize the partitioning by reducing the number of swaps.

B

It has no effect since both conditions yield the same result.

C

It could incorrectly reposition duplicate pivot elements, compromising the partitioning process.

D

It would cause a compilation error due to mismatched types.

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

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy