Skip to main content

Class 52: An Introduction to Sorting

Held:

We explore the problem of *sorting. When you sort a list, vector, or other collection, you put the elements in order. The order of the elements usually corresponds to the type of the elements. We might sort strings alphabetically, grades numerically, colors by brightness, and so on and so forth.*

Preliminaries

Overview

  • The problem of sorting
  • Writing sorting algorithms
  • Some examples
  • Formalizing the problem

Updates

News / Etc.

  • Continue partners.
  • It’s National Pipe Cleaner day! (No, not really.)
  • Next week
    • Monday: We talk about your projects from the design perspective.
    • Tuesday: We talk about your code.
    • Wednesday: We talk about the exam and the class as a whole.
    • Friday: You fill out end-of-course evaluations and get your final PSA.
  • A sponsor is offering to pay for Grinnell Women in Computing shirts for anyone who commits to wearing them. https://grinnell.formstack.com/forms/women_in_computing_shirts

Upcoming Work

  • No more lab writeups.
  • No more quizzes.
  • One more reading: Sorting
  • Exam 4 due Tuesday.

Extra credit (Academic/Artistic)

  • CS Table, Tuesday: CS-related comic strips.
  • CS Extras, Thursday: ???

Extra credit (Peer)

  • Grinnell Monologues is performing this Friday in Main Lounge at 7:30 p.m.
  • Drill Team Show, Friday, May 5, at Triple V stables. The other side of the golf course. Free rides (cars, not pony) to the event!
  • Swim Team hosts Triathalon this weekend (running 3 miles, biking 13 miles, swimming 1/2 mile). Participate or help out.
    • Sam will reimburse your admission fee.
  • GSEA (Grinnell Space Exploration Agency) is launching a weather balloon on May 7th at 5:30AM. The balloon will go to almost-space and take pictures. It’ll also tell us about its location and the temperature of the air around it as it rises. We’re inviting people to come see the launch, if they want to come (despite the fact that it’s at 5:30AM).
  • Baseball, Sunday, May 7 (Senior Day)
  • Advanced Performance Performances Sunday at 7pm in the Wall

Other good things to do

  • Collegium Sunday at 2 p.m.
  • Take care of yourselves.

The Problem of Sorting

  • As we saw recently, one problem that seems to crop up a lot in programmming (and elsewhere) is that of sorting.
  • The problem: Given a list, array, vector, sequence, or file of comparable elements, put the elements in order.
    • In order typically means that each element is no bigger than the next element. (You can also sort in decreasing order, in which case each element is no smaller than the next element.)
  • We’ll look at techniques for sorting vectors and lists.

Designing Sorting Algorithms

  • I suggest that you think about the development of sorting algorithms in Scheme similarly to the way you think about writing many algorithms.
  • Start by thinking about the way you might do it by hand.
    • We may find a few different ways to sort by hand.
    • We’ll probably leave the Scheme-ification to the end.
  • Generalize what you’re doing.
    • What is the “philosophy” of your technique?
    • What are the key steps.
  • Come up with some initial test cases.
  • Consider whether there are any deficiences to your technique.
  • Decide on your parameters (e.g., are you sorting a list or a vector).
  • Translate your algorithm into Scheme.
  • Test test test.

Sample Sorting Algorithms

=== Insertion Sort

  • One simple sorting technique is insertion sort.
  • Insertion sort operates by segmenting the list into unsorted and sorted portions, and repeatedly removing the first element from the unsorted portion and inserting it into the correct place in the sorted portion.
  • This may be likened to the way typical card players sort their hands.
  • How might we code this recursively?
  • Does our code differ for lists and arrays?

=== Selection Sort

  • Selection sort is among the simpler and more natural methods for sorting vectors.
  • In this sorting algorithm, you segment the vector into two subparts, a sorted part and an unsorted part. You repeatedly find the largest of the unsorted elements, and swap it into the beginning of the sorted part. This swapping continues until there are no unsorted elements.
  • Here’s a potentially-helpful picture:
+---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | Unsorted Sorted
  • Note that we can also write selection sort iteratively.

A More Formal Description

  • Before implementing these algorithms, let’s take a look at the way we might document one (or all) of the procedures
    • Purpose?
    • Parameters?
    • Produces?
    • Preconditions?
    • Postconditions?
  • Here are some postconditions I typically think about:
    • You also need to ensure that all elements in the original list are in the sorted list.
    • You also need to ensure that no other elements are in the list.