Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
medium Solved by 1 students

Recursive Merge Sort Implementation

Mark
< Prev
Next >

The merge sort algorithm sorts an array by recursively dividing it into two halves and sorting each half. Which of the following code segments correctly implements these recursive steps?

A

public static void sort(int[] arr) { int n = arr.length; for(int i = 0; i < n; i++) { for(int j = 1; j < (n-i); j++) { if(arr[j-1] > arr[j]) { int temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } }

B

public static void mergeSort(int[] arr, int left, int right) { if(left < right) { int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); // merge(arr, left, mid, right) would merge the two sorted halves } }

C

while(true) { }

D

public static void mergeSort(int[] arr) { for(int i = 0; i < arr.length; i++) { System.out.print(arr[i]); } }

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|