Functional Problem Solving (CSC 151 2016S) : EBoards

CSC151.02 2016S, Class 51: Insertion Sort


Overview

Preliminaries

Admin

Reminders

Upcoming Work:

Extra Credit

Academic / Artistic

Peer

Regular Peer

Misc

Questions

Preparation: A Few Questions on Insertion Sort

Did we talk about this sorting algorithm yesterday?

Nothing quite like this.

What is the key idea in insertion sort?

Two groups of items - Those that are alreay sorted (in order)

Then insert items from unsorted group into the correct place in the sorted group.

Problem: How do you put them in between?

See the insert-number algirthm.

How does insertion sort differ in lists and vectors?

For lists, we have two separate lists and keep building a new sorted list each time.

For vectors, we do it in place and keep track of an "imaginary boundary"

The imaginary boundary is an index in the vector that separates the portion that we know is sorted from the portion that we know is unsorted.

We have to "shift right" to put a new element in.

Why do we provide you with three different kinds of list-based insertion sort (and four kinds of insertion sort)?

One handled only numbers.

One of them uses a predicate, so it could deal with any type.

There was a third one that used a "get key" to tell what were sorting by.

Why would we use a "get-key"?

    (define list-keyed-insertion-sort
      (lambda (lst get-key may-precede?)

Example: We are sorting students. (LNAME FNAME FAVECOLOR BOX HOMELOG HOMELAT)

When sorting, we might say "We're sorting by box number". get-key is then (r-s list-ref 3). may-precede? is <=.

Lab