---
title: Eboard 37  Insertion Sort
number: 37
section: eboards
held: 2018-04-30
link: true
---
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

* [Project](../assignments/project) due tomorrow night!
* [Lab writeup for class 37](../writeups/writeup37): Exercise 5.
  Due Wednesday before class.
* [Flash Cards](../flashcards/flashcards13) due Wednesday at 9pm.
    * Optional.
    * Grade is percent of eight flashcard assignments you complete 
      (capped at 100%).
* Reading for Wednesday: [Merge sort](../readings/merge-sort)

### 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?_

