CS106X
Handout 21
Autumn 2010
October 15
th
, 2010
Assignment 4: Sort Lab
Kudos to Julie Zelenski and Eric Roberts for the handout.
Sorting is one of the most fundamental algorithmic problems within computer science. It
has been claimed that as many as 25% of all CPU cycles are spent sorting, which provides
a lot of incentive for further study.
In addition, there are many other tasks (searching,
calculating the median, finding the mode, remove duplicates, etc.) that can be
implemented much more efficiently once the data is sorted.
Sorting is spectacular in that
there are many varied approaches (divide-and-conquer, partitioning, pigeonhole, etc.) that
lead to useful sorting algorithms, and those techniques often can be repurposed to other
algorithmic tasks. The wide variety of algorithms gives us a lot of richness to explore,
especially when considering the tradeoffs offered in terms of efficiency, operation mix,
code complexity, best/worse case inputs, and so on.
Your next task is to do some
exploration into sorting algorithms in a playful, experimental way— I hope you enjoy it!
Due: Friday, October 22
nd
at 5:00 p.m.
Part A — Sort Detective
Back in the day, students in high school chemistry class were often given an unknown
compound and asked to figure out what it was in a process called qualitative analysis. In
this problem, your job is to take several unknown algorithms and figure out what they are
by experimenting with their computational properties in much the same way.
We give you a compiled library containing five sorting routines, named
MysterySort1
,
MysterySort2
, and so on. Each function is designed to take a vector of numbers and
order the elements into ascending order. What’s different about them is that each uses a
different sorting algorithm. More specifically, the algorithms used are (in alphabetical
order):
• Bubble sort
• Insertion sort
• Merge sort
• Quicksort, where first element of subvector is used as pivot
• Selection sort
We talked about four of these algorithms in class and they are covered in Chapter 8 of the
reader (Insertion Sort appears as exercise 8-2). The only missing algorithm is bubble sort.
Bubble sort is an interesting bit of computer science history. It is a simple O(N
2
) algorithm
that works by repeatedly stepping through the elements to be sorted, comparing two items
at a time, swapping them if they are out of order. The pass through the elements is repeated
until no swaps are needed, which means the data is sorted. The algorithm gets its name
from the way elements "bubble" to the correct position.