Recursive Merge Sort Implementation
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?
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; } } } }
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 } }
while(true) { }
public static void mergeSort(int[] arr) { for(int i = 0; i < arr.length; i++) { System.out.print(arr[i]); } }
Question Leaderboard
Not enough data yet to show leaderboard.
