Partition Sort

PG: Scrap. Scrap all. Two reasons. (1) While sorting algorithms is an important and traditional CS topic, it's not obvious to me why they have any important role in an attract-them-in course. These are TiF for interested kids. (2) If our reason for this unit is to teach recursion, sorting is a distraction, and harder than we need to be. If anything at all, it's a culmination, not a start. I'd be happier showing kids how to march down a list and act on it. With HoFs or recursion. Power and utility. That's currently in Lab 4. Maybe earlier even than this unit?

BH: Definitely replace with mergesort. Partition sort isn't guaranteed O(n log n); its analysis was awarded a PhD at Stanford. Mergesort is straightforward.

MF: I said keep, but I find these pages to be almost to much, and I want to review them. Also, I want to think about whether it would make sense to revisit timing and efficiency here.

Another commonly used sort is called a partition sort. Here's how it works:

Names aren't sorted while placing them before or after a pivot. For list{Emma, Olivia, Sophia, Isabella, Ava, Mia, Emily, Abigail}, this is the status after the first split via pivoting: the pivot is Emma, the list before the pivot is {Ava, Abigail}, and the list after the pivot is {Olivia, Sophia, Isabella, Mia, Emily}. The sorting process then continues on these two smaller unsorted lists.
  1. Explore the partition sort before writing any code. With your class, stand up and form a line. Then, follow the partition sort process to put everyone in sorted order, low to high, by the number of their home address.
  2. How is a partition sort an example of recursion? What is the base case?
We may want to make our own video to show this in action. The linked video is not quite right and has been killed off.

To write a partition sort block in Snap!, you'll need code for a base case, and code that follows the steps of the partition sort.

    Obsolete Click here to load a starter project. Then save it.
    Examine this new project containing the append block (Variables menu). append(list{Ava, Abigail})(list{Emma})(list{Olivia, Sophia, Isabella, Mia, Emily})
    Describe what the append block does.
  1. You'll need to figure out how to place the items into the three categories, and how to create one final list to output.
    Build the recursive reporter partition sort. Its output, for any list, should be the same as the output for selection sort.
For example, let's say yesterday you had a big sorted list, and today you add a few new items, so you need to re-sort the list. A general-purpose sorting algorithm won't take advantage of the almost-in-order character of its input. But insertion sort, an algorithm that's slow in general, handles this special case quickly.

Why are there different sorting algorithms? Some sorting algorithms take longer than others to run, but even an algorithm that's slow in general may be useful in special cases.

  1. You could also use a long list of names here.
    Make a 500-item list of random numbers, and then sort the list using the three sorting algorithms you built in this lab. Keep track of the timings:
    set(biglist) to (Empty list); repeat(500){add(pick random(1) to (2000) to (biglist)}; reset timer; say(sort(biglist)); set(time) to (timer)
  2. Which algorithm was fastest? Why might this type of algorithm run faster?