CSC 151.01, Class 37: An introduction to sorting
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- The problem of sorting
- Writing sorting algorithms
- Some examples
- Formalizing the problem
News / Etc.
- I hope you had a happy Thanksgiving.
- I’ve graded exam 3, but have not sent all of the exams back yet. You should get it back some time today, depending on when I have a spare moment.
- Draft of exam 4 is released.
Upcoming Work
- No writeup for today’s class.
- Projects due Tuesday.
- Reading for Wednesday: Sorting
- Not yet available, probably tonight.
- Exam 4 due in a week.
Extra credit (Academic/Artistic)
- CS Table Tuesday: Unknown topic
- CS Extras Thursday: Unknown topic
Extra credit (Peer)
- Concert Wednesday at 7 p.m. in SL
- Jazz Ensemble Concert Friday at 7:30 p.m.
- Chamber Ensembles Sunday at 4:00 p.m.
Extra credit (Misc)
- Forum: A Community Responds — A Bias/Hate Incident Raising Issues of Free Speech and Inclusivity. Tuesday, 11 a.m., Sebring-Lewis.
- Mental Health Campus Resource Fair. Friday at 4pm in JRC 209.
Other good things
- Swim meet this coming weekend.
- Collegium Concert Sunday at 2:00 p.m.
Questions
- What goes in the project report?
- Whoops. I didn’t mean to be ambiguous. How about: A description of what you achieved. A human-readable explanation of a core algorithm. Instructions for running the things. What you’d do if you had another month.
The problem of sorting
You’ve been using a procedure called sort. The procedure puts things
in order. It’s really useful if you plan to do binary search. But
it’s also useful for many other purposes, such as finding extreme values.
Computer scientists have designed hundreds of different sorting algorithms. We’ll look at a few. (And perhaps even design a few of our own.)
Writing sorting algorithms
A common strategy for solving algorithms: “Do it by hand” and then figure out what we did, generalize it, try it on another example, express it for the computer, and test.
Soon, we will have a group of your colleagues at the front of the room. Each will get an assigned number. Your goal is to put them in order from smallest number to largest number.
Write instructions for putting the people in order.
Some examples
Algorithm 1: Bubble sort
Run through the list, swapping things that are out of order
- This gets us more sorted, but not completely sorted.
- It also put the largest thing at the end.
If I have 10 people, how many times might I have to run through the list? About nine, since once the ninth person is in place, the 10th person is in place.
So the total number of comparisons is approximately n -1 * n-1 for a list of n things. About n*n
Idea: Repeatedly “bubble” elements to the end.
Algorithm 2: Selection sort
- Find the smallest thing by pulling out the first and repeatedly comparing.
- Find the next smallest thing
- And the next
- And the next
- n-1 + n-2 + n-3 + n-4 …
- About n*n/2
Idea: Repeatedly select largest/smallest
- Tends to have fewer swaps than bubble sort.
Algorithm 3: Insertion sort
- Start a new list with the first person
- Repeatedly grab a person and put them into the correct place in the new list.
Questions
- How long does it take to put someone in the right place in a vector
or list? (How many comparisons or other key operations?)
- If we only measure comparisons, finding the correct place should be log2n (the number of times you divide n in half to get 1.)
- But putting something in the middle of a vector or list is
expensive.
- List: cdr through the list to find the right place. (And it’s not a list if we’re doing binary search.)
- Vector,option 1: Create a new vector, copy the first k elements add the new element, copy the remaining elements. If we had n elements, that’s n steps.
- Vector, option 2: Shift everything right in the vector so that there’s room About n steps.
- Another nn (or nn/2 or something similar algorithm)
Idea: Repeatedly insert into a new list/vector.
More ways to sort
Coming Friday
Formalizing the problem
All the sorting methods do the same thing. So they can share documentation. The one difference is whether we think about sorting a vector or sorting a list. Let’s write documentation
;;; Procedure:
;;; vector-sort!
;;; Parameters:
;;; vec, a vector
;;; may-precede?, a binary comparator
;;; Purpose:
;;; Sort vec.
;;; Produces:
;;; vec, the same vector (now mutated)
;;; Preconditions:
;;; * `may-precede?` must be applicable to any two elements in vec.
;;; * `may-precede?` must be transitive. If `(may-precede? a b)` and
;;; `(may-precede? b c)`, then `(may-precede? a c)`.
;;; Postconditions:
;;; * The elements in vec are in order by `may-precede?`
;;; For any two positions, i and j where i < j
;;; `(may-precede? (vector-ref vec i) (vector-ref vec j))`
;;; * The elements in the vector are permuted, but no elements are
;;; added or removed.
;;; Procedure:
;;; list-sort
;;; Parameters:
;;; lst, a list
;;; may-precede?, a binary comparator
;;; Purpose:
;;; Sort lst
;;; Produces:
;;; result, a new list
;;; Preconditions:
;;; * `may-precede?` must be applicable to any two elements in lst.
;;; * `may-precede?` must be transitive. If `(may-precede? a b)` and
;;; `(may-precede? b c)`, then `(may-precede? a c)`.
;;; Postconditions:
;;;
Other good names for the comparator
- less-than is not horrible
- comes-after is good
- comes-before
Naming the result of vector-sort!
vec, since it’s the same vec- [No result], since we’re focusing on the side effect.
What can may-precede? be?
- Something predefined in Scheme, like
< - Something I define, like
(lambda (a b) (< (abs a) (abs b)))
Improving search
We have three O(n*n) algorithms. Can we do better?
Idea: Use ideas from going from linear search to binary search.
- Approach one: Find the median (how?). Divide in half according to the elements less than the median and the elements greater than the median. Sort both halves the same way. “Quicksort”
- Approach two: Take the first half and sort it. Take the second half and sort it. And then? …. “Merge sort”