---
title: Eboard 36  Insertion Sort
number: 36
section: eboards
held: 2019-05-01
link: true
---
CSC 151 2019S, Class 36:  Insertion Sort
========================================

_Overview_

* Preliminaries
    * Notes and news
    * Upcoming work
    * Extra credit
    * Questions
* Some questions about insertion sort
* Lab

Preliminaries
-------------

### News / Etc.

* Mentor sessions Thursday 7-8 p.m., Thursday 8-9 p.m.

### Upcoming work

* Reading for Friday:
  [Merge sort](../readings/mergesort)
* Lab writeup: TBD
* Friday's quiz: Searching and sorting
* Flash cards **Tonight**: Searching and sorting
    * Our last flash cards!
* [Exam 3](../exams/exam03) due next Tuesday.
    * I've made some changes to problem 6 since Monday.
    * Prologue due Friday night.

### Extra Credit

_I would certainly appreciate suggestions of other extra credit activities
(preferably via email)._

#### Extra credit (Academic/Artistic)

* Three talks by Prof. Dr. Yvonne Foerster (<https://yvonnefoerster.com/>)
    * **Today**: May 1, 4:30-6pm, HSSC S3325: _Beyond the Anthropocene: Technology, Innovation, and the (Post-)Human Condition_
    * Thursday, May 2, Noon-12:50pm, HSSC N3110 _Degrees of Freedom: Embodiment, Neuroplasticity, and the Need for a Critical Neuroscience_ (Lunch and beverages provided)
    * Friday, May 3, Noon-12:50pm, Bucksbaum 152: _Designing Future Bodies: Fashion and Technology_ (Lunch and beverages provided)

#### Extra credit (Peer)

* **Tomorrow**: Open Mike, Bob's, 9pm
* Sunday, 2pm, Herrick: Singers and Orchestra: Great Mass in C Minor
    * "Great" means "really long"
* Voice Recital, 7:30 pm, May 10, in Sebring Lewis

#### Extra credit (Wellness)

* **New**: CS Picnic Friday.  Sign up today.

#### Extra credit (Wellness, Regular)

* 30 Minutes of Mindfulness at SHAW every Monday 4:15-4:45
* Any organized exercise.
* 60 minutes of some solitary self-care activities that are unrelated to
  academics or work.  Your email reflection must explain how the activity
  contributed to your wellness.
* 60 minutes of some shared self-care activity with friends. Your email
  reflection must explain how the activity contributed to your wellness.

#### Extra credit (Misc)

### Other good things 

* The Grinnellian: Student concert, next Saturday.

### Questions

_Can you add office hours?_

> I don't really have additional time.  I'm sorry.  I'll try email.

Some questions about insertion sort
-----------------------------------

_What are typical preconditions and postconditions for a procedure that sorts lists?_ `(sort lst less-equal?)`.  You may refer to the return value as `sorted`.

```drracket
;;; Preconditions:
;;;   * lst is not null (some algorithms assume at least one element)
;;;   * All the elements of the list have to be amenable to less-equal?
;;;     (E.g., if less-equal? is <=, they must all be real; if less-equal?
;;;     is string-ci<=?, they must all be strings)
;;;   * less-equal? is transitive.  That is, if (less-equal? a b) and
;;;     (less-equal b c), then (less-equal? a c).
;;;   * less-equal? is consistent.  That is, if (less-equal? a b) at one
;;;     point, (less-equal? a b) holds at all subsequent points.
;;; Postconditions:
;;;   * All adjacent pairs are in order.  
;;;     (less-equal? (list-ref sorted i) (list-ref sorted (+ 1 i)))
;;;   * Note: By transivity, each element is less than or equal to
;;;     all subsequent elements.
;;;   * sorted is a permutation of lst.  We neither added nor removed
;;;     elements
```

_What's a non-transitive `less-equal?`?_

```drracket
(define my-comparison?
  (lambda (a b)
    (zero? (random 2))))

(define favorite?
  (lambda (a b)
    (or (and (equal? a "first") (equal? b "second"))
        (and (equal? a "second") (equal? b "third"))
        (and (equal? a "third") (equal? b "first")))))
```

_What are typical preconditions and postconditions for a procedure that sorts vectors?_ `(vector-sort! vec less-equal?)`.  There is no return value.

;;; Preconditions:
;;;   * vec is not empty (some algorithms assume at least one element)
;;;   * All the elements of the vector have to be amenable to less-equal?
;;;     (E.g., if less-equal? is <=, they must all be real; if less-equal?
;;;     is string-ci<=?, they must all be strings)
;;;   * less-equal? is transitive.  That is, if (less-equal? a b) and
;;;     (less-equal b c), then (less-equal? a c).
;;;   * less-equal? is consistent.  That is, if (less-equal? a b) at one
;;;     point, (less-equal? a b) holds at all subsequent points.
;;; Postconditions:
;;;   * All adjacent pairs are in order.  
;;;     (less-equal? (vec-ref vec i) (vec-ref vec (+ 1 i)))
;;;   * Note: By transivity, each element is less than or equal to
;;;     all subsequent elements.
;;;   * We have neither added to nor removed elements from the vector
;;;     (The vector contains a permutation of its original elements.)
```

_How would you document the following procedure?_


```drracket
;;; Procedure:
;;;   swap!
;;; Parameters:
;;;   vec, a vector
;;;   a, a non-negative integer
;;;   b, a non-negative integer
;;; Purpose:
;;;   Swaps the two elements of the vector at indices a and b.
;;; Produces:
;;;   [Nothing; called for the side effect]
;;; Preconditions:
;;;   * 0 <= a < (vector-length vec)
;;;   * 0 <= b < (vector-length vec)
;;;   * A = (vector-ref vec a)
;;;   * B = (vector-ref vec b)
;;; Postconditions:
;;;   * The length of the vector hasn't changed.
;;;   * For all i, 0 <= i < (vector-length vec); if i is not a and i is
;;;     not b, then (vector-ref vec i) is unchanged.
;;;   * (vector-ref vec a) = B
;;;   * (vector-ref vec b) = A
;;;   * "The elements at positions a and b swapped."
(define swap!
  (lambda (vec a b)
    (let ([val (vector-ref vec a)])
      (vector-set! vec a (vector-ref vec b))
      (vector-set! vec b val))))
```

_What is the basic idea of insertion sort?_

* We divide our world into two parts: sorted and unsorted
* Repeatedly remove one element from unsorted and put it into the
  right place in sorted.
* In vectors, we designate a boundary in the vector

        +--------------+-----------------------+
        |  sorted      |   unsorted            |
        +--------------+-----------------------+

_Why does `vector-insert!` take four parameters?_

```drracket
(define vector-insert!
  (lambda (vec new-element boundary less-equal?)
    ...))
```

* `vec` is the vector we are working with
* `new-element` is the new element we are inserting
* `boundary` is the boundary between the sorted section and the
  unsorted section.
* `less-equal?` is the same thing it's always been, the criterion by
  which we order values

An Example

        +---+---+---+---+---+---+---+---+
        | 7 | 1 | 4 | 6 | 5 | 0 | 2 | 3 |
        +---+---+---+---+---+---+---+---+
            |

        +---+---+---+---+---+---+---+---+
        | 1 | 7 | 4 | 6 | 5 | 0 | 2 | 3 |
        +---+---+---+---+---+---+---+---+
                |

        +---+---+---+---+---+---+---+---+
        | 1 | 4 | 7 | 6 | 5 | 0 | 2 | 3 |
        +---+---+---+---+---+---+---+---+
                    |

        +---+---+---+---+---+---+---+---+
        | 1 | 4 | 6 | 7 | 5 | 0 | 2 | 3 |
        +---+---+---+---+---+---+---+---+
                        |

        +---+---+---+---+---+---+---+---+
        | 1 | 4 | 5 | 6 | 7 | 0 | 2 | 3 |
        +---+---+---+---+---+---+---+---+
                            |

Lab
---
