Quicksort is akin to dividing a file into two parts
Algorithm quicksort(array)
partition(array)
quicksort(lefthalf)
quicksort(righthalf)
Mergesort is akin to combining two ordered files into one larger ordered file
Algorithm mergesort(array)
mergesort(lefthalf)
mergesort(righthalf)
merge(lefthalf, righthalf)
This was made for sorting things on magnetic tapes. And the thing about magnetic tapes was that they were not good for random access. Items are accessed sequentially or in large blocks. You need enough memory for at least one thing while sorting.
void merge(Item c[], Item a[], int size_a, Item b[], int size_b) {
for (int i = 0, j = 0, k = 0; k < size_a + size_b; k++) {
if (i == size_a)
c[k] = b[j++];
else if (j == size_b)
c[k] = a[i++];
else
c[k] = (a[i] <= b[j]) ? a[i++] : b[j++];
}
}
The runtime of this is , where is the size of A plus the size of B.
Advantages:
Disadvantages: