Merge Sort
Merge Sort
It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then
merging them back together to obtain the sorted array.
mid = length of array / 2 while leftIndex < length of left and rightIndex < length of right
leftHalf = array[0...mid-1] if left[leftIndex] <= right[rightIndex]
rightHalf = array[mid...end] append left[leftIndex] to result
leftIndex = leftIndex + 1
sortedLeft = mergeSort(leftHalf) else
sortedRight = mergeSort(rightHalf) append right[rightIndex] to result
rightIndex = rightIndex + 1
return merge(sortedLeft, sortedRight)
while leftIndex < length of left
append left[leftIndex] to result
This pseudocode breaks down the Merge Sort leftIndex = leftIndex + 1
algorithm into two main functions: mergeSort
and merge. The mergeSort function recursively while rightIndex < length of right
splits the array into halves until it reaches arrays append right[rightIndex] to result
of length 1 or 0. The merge function then rightIndex = rightIndex + 1
combines these smaller arrays back together in
sorted order. return result
PROGRAM TO MPLEMENT MERGE SORT