Skip to main content

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
  • Lab writeup: TBD
  • Friday’s quiz: Searching and sorting
  • Flash cards Tonight: Searching and sorting
    • Our last flash cards!
  • Exam 3 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.

;;; 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??

(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?

(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