by

Version 1 (October 9, 2018)

Download (9 downloads)

A sample implementation of the MergeSort algorithm.

MergeSort is a divide and conquer sorting algorithm. It first checks if the array contains at most 1 element - that means it's already sorted. Otherwise it splits the array into 2 halves, sorts each one by MergeSort (this makes it a recursive algorithm) and then "merges" the sorted halves.

Merging is done by comparing the first element of each half and removing and placing the smaller one into a new array. Eventually, this will produce a single array whose elements are sorted in an ascending order.