Functional Problem Solving (CSC 151 2016S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [Remote] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2016S)] [Davis (2013F)] [Rebelsky (2015F)] [Weinman (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
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-numberalgirthm.
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-keyis then(r-s list-ref 3).may-precede?is<=.