Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
hard

Java Quicksort Partition Error

Mark
< 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 pivot should always be chosen as the first element of the array rather than the last element.

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 condition ‘low >= high’ in quickSort is incorrect and should instead be ‘low > high’.

D

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

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.

AI Tutor

How can I help?

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