Merge Sort
Merge sort follows divide-and-conquer
approach.
Merge Sort
Merging two arrays L[] and R[]
Analysis
Time complexity
Fully expanded recursion tree has logn + 1
levels.
Each level contributes to total cost of cn
Total cost = cn logn + cn = O(nlogn)
Table based comparison
Merge Sort | Heap Sort | Quick Sort |
---|---|---|
Space O(n) | In place |
In place |
Best: O(nlogn) Avg: O(nlogn) Worst: O(nlogn) |
Best: O(nlogn) Avg: O(nlogn) Worst: O(nlogn) |
Best: O(nlogn) Avg: O(nlogn) Worst: O() |
Divide and Conquer | Divide and Conquer | |
Runs better than heapsort in data cache if its on array |
Runs faster in small data cache | Works really well in virtual memory env/Caches |
Access frequent contiguous memory locations |
Spread throughout the heap | Access frequent contiguous memory locations |
Stable | Not Stable | Not Stable |
Parallelize well | Do not Parallelize well | Parallelize well |
Used in external sort | Can not be used in external sort.Locality of ref is issue. |
Used in external sort |