Merge Sort

Structure: Break the list into two equal-size sublists. Recursively sort the two sublists. Merge them together to form a single sorted list.

Tricky part: How do you merge?

Insertion sort is a special case in which we merge a sorted list of size n-1 and one of size 1.

Idea: Look at the first element in each list. Remove the smaller one (by definition, an empty list is never the smaller one). Repeat. Animated example. This involves constant work per element of the final list, i.e., linear time. (To be analyzed later.)

Awkwardness: Not ``in place.''


next up previous
Next: Quicksort Up: DIVIDE-AND-CONQUER SORTING Previous: Two Algorithms