Skip to main content

CSC 151.01, Class 37: Insertion Sort

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Lab
  • Debrief

Preliminaries

News / Etc.

  • New partners.

Upcoming work

Extra credit (Academic/Artistic)

  • CS Table Tuesday (topic tbd)
  • PBK Scholars’ Convocation Thursday
    • Warning! PBK Convos generally involve twenty minutes of induction ceremonies.
  • CLS Public Engagement in Science Symposium
    • “Engaging the Public in Science: Evolution in Action in the Classroom”. Dr. Louise Mead, Education Director of BEACON Center for the Study of Evolution in Action May 3, 4 p.m., JRC 101
    • “Using Human Examples to Teach Evolution: Recent Research in U. S. High School Classrooms” Dr. Briana Pobiner, Smithsonian’s National Museum of Natural History May 3, 7:30 p.m., JRC 101
    • Up Goer Five Challenge Poster Display May 4, 10 a.m. – 5 p.m., Bucksbaum Rotunda
    • Poster Reception May 4, 3 p.m., Bucksbaum Rotunda
    • “Our Journeys: From (Star Wars) Grinnell to Minute Physics & Minute Earth” Alex ’11 and Henry ’09 Reich May 4, 4 p.m., Faulconer Gallery

Extra credit (Peer)

  • Grinnellian this Saturday, May 5 on Commencement Stage. No plays around 5pm.
  • Latin-American Ensemble, Thursday at some time.

Extra credit (Recurring peer)

  • Listen to KDIC Wednesdays at 6pm - Witty banter with other personalities and/or co-host. Also Indian, Arabic, and Farsi music.
    (Up to two units of extra credit.)
  • Listen to KDIC Thursday at 7pm - Classic Rock. (60’s and 70’s)
  • Peer editing with SS. Talk to SS about the details. Make your English Lit more literate.

Extra credit (Misc)

Other good things

  • Vocal recitals Friday.
  • (Lots of other music stuff, too.)

Questions

Can we debrief on the last two procedures in the reading?

  • Conceptually, insertion sort divides the vector into two parts: the things that are sorted and the things that are not yet visited.
  • In class, we said something like
    • Pull out the first unprocessed value
    • Find the appropriate location in the sorted section
    • Shift
    • Put it there.
  • In practice, as long as we’re going to shift, we may just as well shift every element that is larger as soon as we notice it is larger.
  • The insert! procedure does this shifting.
  • Once we have insert!, insertion-sort! just involves calling insert! starting at every position from 1 to length-1.

What does vector-insert! return?

  • Nothing; called for the side effect.

Do we use binary search?

  • No; the shifting is expensive enough that it’s not helpful.

Why did we talk about vector-swap!?

  • It’s useful for selection sort, another sorting routine.
  • It’s also generally useful.

Why does vector-insert! take four parameters?

  • vector into which we are inserting the value
  • value we are inserting
  • the position we just emptied (see below)
  • comparator

What does it mean to have “no value” in a position in a vector?

  • The “no value” is conceptual, rather than actual. There’s still a value there. We just pretend that there isn’t.

Lab

Writeup: Exercise 5.

How do I write a procedure that returns the same value it got as input?

  • (lambda (x) ...)
  • You should be able to figure out the ellipses.

Debrief

What do you see as the big differences between the list-based insertion sort and the vector-based insertion sort?

What ordering of elements in a list makes insertion sort fast?

What ordering of elements in a vector makes insertion sort fast?

What if we sort a “random” list or vector of elements. Will the number of steps be closer to to the fast ordering or the slow ordering? Can you estimate the number of steps?