Merge-sort
Learn the basics of sorting and the merge-sort.
We'll cover the following
There are the three sorting algorithms:
- Merge-sort
- Quicksort
- Heap-sort
Each of these algorithms takes an input array a
and sorts the elements of a
into non-decreasing order in (expected) time. These algorithms are all comparison-based. These algorithms don’t care what type of data is being sorted; the only operation they do on the data is comparisons using the compare(a, b)
method. Recall that compare(a, b)
returns a negative value if , a positive value if , and zero if .
This module discusses algorithms for sorting a set of items. This might
seem like a strange topic for a course on data structures, but there are several good reasons for including it here. The most obvious reason is that two of these sorting algorithms (quicksort and heap-sort) are intimately related to two of the data structures we have already studied (random
binary search trees and heaps, respectively).
Basics of sorting
There are several algorithms that sort using only comparisons and they run in time. As it turns out, all three algorithms are asymptotically optimal; no algorithm that uses only comparisons can avoid doing roughly comparisons in the worst case and even the average case.
Before continuing, we should note that any of the Set or priority
Queue implementations can also be used
to obtain an time sorting algorithm. For example, we can sort items by performing add(x)
operations followed by remove()
operations on a BinaryHeap
or MeldableHeap
. Alternatively, we can use add(x)
operations on any of the binary search tree data structures and then perform an in-order traversal to extract the elements in sorted order. However, in both cases we go through a lot of overhead to build a structure that is never fully used. Sorting is such an important problem that it is worthwhile developing direct methods that are as fast, simple, and space-efficient as possible.
If we allow other operations besides comparisons, then all bets are off. Indeed, by using array indexing, it is possible to sort a set of integers in the range in time.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy