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
hard Solved by 4 students

QuickSort Partition Method Error

Mark
< Prev
Next >

The partition method below is intended for use as part of the QuickSort algorithm. Which statement best describes a logical error in this implementation?

public class QuickSortExample {
    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

The index ‘i’ should be initialized to ‘low + 1’ to correctly manage the swap positions.

B

The pivot should be chosen from the beginning of the array (arr[low]) rather than from the end.

C

The for-loop uses ‘j <= high’ instead of ‘j < high’, causing the pivot element (at arr[high]) to be processed within the loop.

D

The final swap is redundant because the pivot is already in the correct location.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1hiboyshello22 - 200
#2vld8kitty11 0m 35s 65
#3mattaana00011 1m 15s 25
#4lsj0803092202 - -20
#5busemagngr01 0m 26s -36
Items per page:
10
1 – 5 of 5
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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