Merge Sort
Last updated
Was this helpful?
Last updated
Was this helpful?
Merge Sort is a comparison algorithm that tries to sort the dataset by a Divide And Conquer method. The performance of this algorithm is $O(n*log(n))$.
We start by splitting the array up into separate elements, then we start by merging pairs together and we make sure that the smallest element is always on the left (this makes sure that our first index on the end is correct too). If the array start getting bigger then 2 elements, then we join those larger arrays by going through their elements one by one and putting the smallest one first.
For the implementation check paragraph §5.4
.
Advantages
Stable
$\Theta(n*lg(n))$ Performance
Doesn't need random access to data
Disadvantages
Not adaptive
$\Theta(n)$ extra space normal
$\Theta(lg(n))$ Extra space for LinkedLists
Worst Case
Average Case
Best Case
O(n log n)
O(n log n)
O(n log n)
For the performance of merge sort we will go out from the assumption that $n = 2^k$. This is because we can then split the table in 2 every time.
$T(n) = 2T(n / 2) + cn$
cn stands for the number of operations needed for merging. This is because merging 2 sub tables is $\Theta(n_1 + n_2)$. Now to solve this equation we will just divide both terms by n.
And we keep on doing this (recursion) because we need the table to become 1 element big.
On the end we just add them both together and we get:
Implementing this algorithm is not so hard if you know how to, you need to keep 2 methods in mind:
Merge (Combines 2 arrays with each other)
MergeSort (Main method, accepts a dataset, splits it and calls this method for those 2 splitted parts)
Then just recursively call MergeSort and you are done.
100
1.000
10.000
100.000
1.000.000
Random Elements
0.000163
0.001637
0.018098
0.19313
2.01314
Ascending Elements
0.000145
0.001474
0.016712
0.172718
1.85952
Descending Elements
0.00015
0.001472
0.016825
0.173975
1.84874
When sorting a Linked List Mergesort is the best choice, however for normal arrays it is better to use other algorithms such as heap sort (which requires less space) or quicksort.
Because is a constant we can say that $T(n) = O(n*lg(n))$. Also when n is not a power of 2, then we get the same result.