Nothing delights me more than great books.
If you like my blog, I'd be thankful for a gift from my Amazon book wishlist. :)
This is the third post in an article series about MIT’s lecture course “Introduction to Algorithms.” In this post I will review lectures four and five, which are on the topic of sorting.
The previous post covered a lecture on “Divide and Conquer” algorithm design technique and its applications.
Lecture four is devoted entirely to a single sorting algorithm which uses this technique. The algorithm I am talking about is the “Quicksort” algorithm. The quicksort algorithm was invented by Charles Hoare in 1962 and it is the most widely used sorting algorithm in practice.
I wrote about quicksort before in “Three Beautiful Quicksorts” post, where its running time was analyzed experimentally. This lecture does it theoretically.
Lecture five talks about theoretical running time limits of sorting using comparisons and then discusses two linear time sorting algorithms — Counting sort and Radix sort.
Lecture 4: Quicksort
The lecture starts by giving the divide and conquer description of the algorithm:
- Divide: Partition the array to be sorted into two subarrays around pivot x, such that elements in the lower subarray <= x, and elements in the upper subarray >= x.
- Conquer: Recursively sort lower and upper subarrays using quicksort.
- Combine: Since the subarrays are sorted in place, no work is needed to combine them. The entire array is now sorted!
The main algorithm can be written in a few lines of code:
Quicksort(A, p, r) // sorts list A[p..r]
if p < r
q = Partition(A, p, r)
Quicksort(A, p, q-1)
Quicksort(A, q+1, r)
The key in this algorithm is the Partition subroutine:
Partition(A, p, q)
// partitions elements in array A[p..q] in-place around element x = A[p],
// so that A[p..i] <= x and A[i] == x and A[i+1..q] >= x
x = A[p]
i = p
for j = p+1 to q
if A[j] <= x
i = i+1
swap(A[i], A[j])
swap(A[p], A[i])
return i
The lecture then proceeds with the analysis of Quicksort’s worst-case running time. It is concluded that if the array to be sorted is already sorted or already reverse sorted, the running time is O(n2), which is no better than Insertion sort (seen in lecture one)! What happens in the worst case is that all the elements get partitioned to one side of the chosen pivot. For example, given an array (1, 2, 3, 4), the partition algorithm chooses 1 as the pivot, then all the elements stay where they were, and no partitioning actually happens. To overcome this problem Randomized Quicksort algorithm is introduced.
The main idea of randomized quicksort hides in the Partition subroutine. Instead of partitioning around the first element, it partition around a random element in the array!
Nice things about randomized quicksort are:
- Its running time is independent of initial element order.
- No assumptions need to be made about the statistical distribution of input.
- No specific input elicits the worst case behavior.
- The worst case is determined only by the output of a random-number generator.
Randomized-Partition(A, p, q)
swap(A[p], A[rand(p,q)]) // the only thing changed in the original Partition subroutine!
x = A[p]
i = p
for j = p+1 to q
if A[j] <= x
i = i+1
swap(A[i], A[j])
swap(A[p], A[i])
return i
The rest of the lecture is very math-heavy and uses indicator random variables to conclude that the expected running time of randomized quicksort is O(n·lg(n)).
In practice quicksort is 3 or more times faster than merge sort.
Please see Three Beautiful Quicksorts post for more information about the version of industrial-strength quicksort.
You’re welcome to watch lecture four:
Topics covered in lecture four:
- [00:35] Introduction to quicksort.
- [02:30] Divide and conquer approach to quicksort.
- [05:20] Key idea - linear time (Θ(n)) partitioning subroutine.
- [07:50] Structure of partitioning algorithm.
- [11:40] Example of partition algorithm run on array (6, 10, 13, 5, 8, 3, 2, 11).
- [16:00] Pseudocode of quicksort algorithm.
- [19:00] Analysis of quicksort algorithm.
- [20:25] Worst case analysis of quicksort.
- [24:15] Recursion tree for worst case.
- [28:55] Best case analysis of quicksort, if partition splits elements in half.
- [28:55] Analysis of quicksort, if partition splits elements in a proportion 1/10 : 9/10.
- [33:33] Recursion tree of this analysis.
- [04:30] Analysis of quicksort, if partition alternates between lucky, unlucky, lucky, unlucky, …
- [46:50] Randomized quicksort.
- [51:10] Analysis of randomized quicksort.
Lecture four notes:
Lecture 5: Lower Sorting Bounds and Linear Sorting
The lecture starts with a question — How fast can we sort? Erik Demaine (professor) answers and says that it depends on the model of what you can do with the elements.
The previous lectures introduced several algorithms that can sort n numbers in O(n·lg(n)) time. Merge sort achieves this upper bound in the worst case; quicksort achieves it on average. These algorithms share an interesting property: the sorted order they determine is based only on comparisons between the input elements. Such sorting algorithms are called Comparison Sorts, which is a model for sorting.
It turns out that any comparison sort algorithm can be translated into something that is called a Decision Tree. A decision tree is a full binary tree that represents the comparisons between elements that are performed by a particular sorting algorithm operating on an input of a given size.
Erik uses decision trees to derive the lower bound for running time of comparison based sorting algorithms. The result is that no comparison-based sort can do better than O(n·lg(n)).
The lecture continues with bursting outside of comparison model and looks at sorting in linear time using no comparisons.
The first linear time algorithm covered in the lecture is Counting Sort. The basic idea of counting sort is to determine, for each input element x, the number of elements less than x. This information can be used to place element x directly into its position in the output array. For example, if there are 17 elements less than x, then x belongs in output position 18.
The second linear time algorithm is Radix Sort, which sorts a list of numbers by examining each digit at a given position separately.
Erik ends the lecture by analyzing correctness and running time of radix sort.
Video of lecture five:
Topics covered in lecture five:
- [00:30] How fast can we sort?
- [02:27] Review of running times of quicksort, heapsort, merge sort and insertion sort.
- [04:50] Comparison sorting (model for sorting).
- [06:50] Decision trees.
- [09:35] General description of decision trees.
- [14:25] Decision trees model comparison sorts.
- [20:00] Lower bound on decision tree sorting.
- [31:35] Sorting in linear time.
- [32:30] Counting sort.
- [38:05] Example of counting sort run on an array (4, 1, 3, 4, 3)
- [50:00] Radix sort.
- [56:10] Example of radix sort run on array (329, 457, 657, 839, 546, 720, 355).
- [01:00:30] Correctness of radix sort.
- [01:04:25] Analysis of radix sort.
Lecture five notes:
Have fun sorting! The next post will be about order statistics (given an array find n-th smallest element)!
PS. This course is taught from the CLRS book (also called “Introduction to Algorithms”):
Did you like this post? Subscribe here:
If you really enjoyed the post, I'd appreciate a gift from my geeky Amazon book wishlist. Books would make me more educated and I could write even better posts. Thanks! :)

(7 votes, average: 4.86 out of 5)
|
|
|


August 26th, 2008 at 2:31 pm
Hi, Are you familiar with the `sort pump’? It’s O(n). This Powerpoint file has some detail on it, althought it would help if you can grok Occam. It can be implemented in hardware too. Cheers, Ralph.
August 27th, 2008 at 3:19 pm
[…] MIT’s Introduction to Algorithms, Lectures 4 and 5: Sorting - good coders code, great reuse (tags: programming education computer algorithms algorithm MIT) […]
August 27th, 2008 at 8:15 pm
[…] key idea of this algorithm is to call Randomized-Partition (from previous lecture) subroutine on the given array and then recursively conquer on the partitioned elements on the left […]
August 28th, 2008 at 2:49 am
Ralph, I am not familiar with ‘sort pump‘! Thanks for the presentation!
September 24th, 2008 at 4:17 am
[…] the right branch. Can you see why the printed list of values is sorted? (If not see the lecture ) [part three of the article series covers sorting […]
October 11th, 2008 at 2:34 am
[…] http://duncan99.wordpress.com/?p=4 - bookmarked by 3 members originally found by gvn on 2008-09-19 MIT’s Introduction to Algorithms, Lectures 4 and 5: Sorting http://www.catonmat.net/blog/mit-introduction-to-algorithms-part-three/ - bookmarked by 4 members […]
October 18th, 2008 at 8:30 am
Very good posts - great to see enthusiasm about algorithms. One little - non-algorithmic - point, the inventor of Quicksort, C. A. R. Hoare, is known as “Tony Hoare” - as in Anthony.
October 28th, 2008 at 7:39 am
question on E[X_k]: those MIT students made me nervous of not being able to answer the question. Waste of 30 seconds in the video. Grr.
October 28th, 2008 at 8:47 am
I had trouble with the integral method, but this one says why so it:
here
Basically, to find and upper bound of a sum (from a to b), integrate the f-n from a to b+1 (which makes sense after 2 minutes of looking at those notes).
March 7th, 2009 at 8:33 pm
[…] lecture then proceeds to parallel sorting. The classical sorting algorithms were covered in lectures four and five. This lecture parallelizes the Merge-Sort […]
March 31st, 2009 at 7:51 am
hey!!
can you give me the full detail of how partition sort run!!
thanks!!
need it a lot!!
May 16th, 2009 at 5:39 pm
[…] [upmod] [downmod] MIT’s Introduction to Algorithms, Lectures 4 and 5: Sorting - good coders code, great reuse (www.catonmat.net) 1 points posted 8 months, 3 weeks ago by SixSixSix tags algorithms lectures […]
November 12th, 2009 at 8:41 pm
[…] Lectures 4 and 5: Sorting […]