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 (divideandconquer, 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 82). 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.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2
Here is pseudocode for the bubble sort algorithm:
loop doing passes over the elements until sorted
a pass goes from start to end
compare each adjacent pair of elements
if the two are out of order, swap
if no pairs were exchanged on this pass, you're done
Bubble sort is a rather weak algorithm that for some strange reason dominates introductory
courses (despite the fact that it has just enough subtlety to often be demonstrated with
bugs). Owen Astrachan, a friend and colleague of mine at Duke, once presented a
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Cain,G
 Sort, Bubble Sort, sort insertion sort, Owen Astrachan

Click to edit the document details