Challenge: Implement Merge Sort

The mergeSort() function should recursively sort the subarray array[pr]array[p \cdots r] , i.e., after calling mergeSort(array, p, r) the elements from index p to index r of array should be sorted in ascending order.

To remind you of the merge sort algorithm:

  • If the subarray has size 00 or 11, then it's already sorted, and so nothing needs to be done.

  • Otherwise, merge sort uses divide-and-conquer to sort the subarray.

Use merge(array, p, q, r) to merge sorted sub arrays array[pq]array[p \cdots q] and array[q+1r]array[q+1 \cdots r].

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy