Skip to main content

CSC 151.03, 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 and returned exam 3.
  • Draft of exam 4 is released.

Upcoming Work

  • No writeup for today’s class.
  • Projects due Tuesday.
  • Reading for Wednesday: Sorting
  • Exam 4 due in a week.

Extra credit (Academic/Artistic)

  • CS Table Tuesday: Unknown topic
  • CS Extras Thursday: Unknown topic

Extra credit (Peer)

  • One acts Friday at 8pm and Saturday at 2pm and 8pm and Sunday at 2pm.

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.
  • Jazz Ensemble Concert Friday at 7:30 p.m.
  • Chamber Ensembles Saturday at 4:00 p.m.
  • Collegium Concert Sunday at 2:00 p.m.

Questions

The problem of sorting

Early this semester, we taught you sort, which changes the order of the elements in a list so that they are arranged from “smallest” to “largest”.

It’s a common problem

  • Preparation for binary search.
  • Find the median.
  • Find the nth largest (or nth smallest)

Since it’s a common problem, computer scientists have spent a lot of time designing and studying sorting algorithms.

  • About a dozen core sorting methods, each with variants.
  • Potentially hundreds of different methods.

Writing sorting algorithms

A typical strategy

  • Start by solving an example.
  • Try to generalize.
  • Try on another input
  • Code
  • Test
  • Prove correct

Some examples

How would you put this group of students in order from smallest to largest?

Strategy 1: Selection sort

  • Find the smallest. Put it at the front of a new list.
  • Find the smallest remaining. Put it next.
  • Find the smallest remaining. Put it next.

If there are 10 elements, how many comparisons will our mentor do?

  • 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45

If there are n elements, how many comparisons will our mentor do?

  • n-1 + n-2 + n-3 + …. + 3 + 2 + 1 = n(n-1)/2 or about n*n/2

  • Key step: Select largest/smallest.

Strategy 2: Insertion sort

  • Turn the first element into a list.
  • Add the next element to the right place in the list.
  • Add the next element to the right place.
  • Add the next element to the right place.

How do we find the right place?

  • Look at the elements one by one.
  • We could use binary search to find the right place. That’s fast.
  • Unfortunately, inserting is slow.

How long does that take?

  • Insert the first element: 1 steps
  • Insert the next element: 2 steps
  • Insert the next element: 3 steps

Whoops, it’s the same pattern for figuring out the cost.

Key step: Insert into a list.

Strategy 3: Bubble sort

  • Go through the list, and swap if two elements are out of order.
    • The largest is now at the end of the list.
  • Then do it again
  • And again
  • And again
  • Each time through, we do n-1 comparisons
  • Potentially n-1 repetitions of the big thing
  • So (n-1)*(n-1)

Key idea: Elements “bubble” to their correct position

Formalizing the problem

Let’s document one of the versions of sort.

;;; Procedure:
;;;   list-sort
;;; Parameters:
;;;   lst, a list
;;;   may-precede?, a binary comparator
;;; Purpose:
;;;   sort the list
;;; Produces:
;;;   sorted, a list
;;; Preconditions:
;;;   * may-precede? should be applicable to any two elements in the list
;;;   * may-precede? must be transitive.  If `(may-precede? a b)` and
;;;     `(may-precede? b c)` then `(may-precede a c)`?
;;; Postconditions:
;;;   * sorted is a permutation of lst.
;;;   * For any index, i, 0 <= i < (- length 1),
;;;     `(may-precede? (list-ref lst i) (list-ref lst (+ 1 i)))`

How would the documentation change if we worked with vectors?

  • We might make it mutate the vector instead of creating a new vector.
  • We would change list-ref to vector-ref.
  • We would use the name vec rather than lst.

What should we call the binary comparator?

  • pred? - not very informative.
  • compare? - a bit more informative.
  • may-precede? - more informative Given two values, tells you whether the first can precede the second.
  • less-than? - clearer, except that we allow >

Making it better

All of the algorithms are O(n*n).

We improved linear search to binary search by “divide and conquer”.

Can we similar improve one of our searching algorithms (perhaps by limiting inputs)?