Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
easy

Quicksort Recursive Call Error

Mark
< Prev
Next >

The code below shows an implementation of the quicksort algorithm. The quickSort method, however, contains a logical error that can lead to infinite recursion. Which of the following statements correctly identifies the error?

public class QuickSortOverlap {
    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int p = partition(arr, low, high);
            quickSort(arr, low, p);
            quickSort(arr, p + 1, high);
        }
    }
    
    public static int partition(int[] arr, int low, int high) {
        int pivot = arr[high];
        int i = low - 1;
        for (int j = low; j < high; j++) {
            if(arr[j] <= pivot) {
                i++;
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        int temp = arr[i+1];
        arr[i+1] = arr[high];
        arr[high] = temp;
        return i+1;
    }
}
A

The left recursive call should use ‘quickSort(arr, low, p-1)’ instead of ‘quickSort(arr, low, p)’.

B

The initialization of ‘i’ should be ‘low’ instead of ‘low - 1’.

C

The partition function is incorrectly returning the pivot index.

D

The pivot element should be the first element of the array.

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|Instagram: @_apfive_|Privacy Policy|