| preferred AP College board partner for AP classes
hard
Java Quicksort Partition Error
< Prev
Next >

Consider the following implementation of the Quicksort algorithm.

public class Sorter {
    public static void quickSort(int[] arr, int low, int high) {
        if(low >= high) return;
        int pivotIndex = partition(arr, low, high);
        quickSort(arr, low, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, high);
    }
    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;
    }
}

Which statement best describes a logical error in the provided code?

A

The condition ‘low >= high’ in quickSort is incorrect and should instead be ‘low > high’.

B

The for-loop in the partition method should use ‘j < high’ instead of ‘j <= high’ to avoid comparing the pivot with itself.

C

The recursive calls in quickSort include incorrect indices that cause elements to be skipped.

D

The pivot should always be chosen as the first element of the array rather than the last element.

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