Algorithms and OOD (CSC 207 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Pictures help!
Picture of insertion sort on the board
+--------------+---------------+ | sorted | unsorted | +--------------+---------------+ | | | 0 i length
Picture of selection sort on the board
+--------------+---------------+ |sorted, small | unsorted | +--------------+---------------+ | | | 0 i length
Picture of binary search on the board
+---------+---------+---------+ | < val | unknown | > val | +---------+---------+---------+ | | | | 0 lb ub length
Math (formality) helps, too!
+---------+---------+---------+---------+
| red | white | unknown | blue |
+---------+---------+---------+---------+
| | | | |
0 r i b length
How might we represent this more formally (in Math/Code)
How might we arrange things so that this holds when our algorithm starts? (What values of r, i, and b guarantee those three characteristics?)
r = 0, i = 0, b = length
+---------+---------+---------+---------+ | | +---------+---------+---------+---------+ | | 0 length r b i
Given the following state, what can we do to shrink the size of unknown?
+---------+---------+---------+---------+
| red | white | unknown | blue |
+---------+---------+---------+---------+
| | | | |
0 r i b length
If the value at position i is blue ...
+---------+---------+-----------+---------+
| red | white |b unknown x| blue |
+---------+---------+-----------+---------+
| | | | |
Swap it to right before the blue stuff
+---------+---------+-----------+---------+
| red | white |x unknown b| blue |
+---------+---------+-----------+---------+
| | | | |
Move the blue boundary
+---------+---------+----------+----------+
| red | white |x unknown |b blue |
+---------+---------+----------+----------+
| | | | |
If the element is white, move the white boundary up
+---------+---------+---------+---------+
| red | white |w unknown| blue |
+---------+---------+---------+---------+
| | | | |
0 r i b length
+---------+----------+--------+---------+
| red | white w|unknown | blue |
+---------+----------+--------+---------+
| | | | |
If the element is red, things are a bit more complicated. You'll need to swap the element to the end of the reds and move the red boundary.
+---------+---------+---------+---------+
| red |x white |r unknown| blue |
+---------+---------+---------+---------+
| | | | |
0 r i b length
+-----------+-------+---------+---------+
| red r|white |x unknown| blue |
+-------------------+---------+---------+
| | | | |
0 r i b length
If our goal is to look at each element once, then it's concerning that
we'll have to look at the x we passed by. But that x was in the
white section, so we know it's white and can also increment the white
boundary.
+-----------+---------+-------+---------+
| red r|white w|unknown| blue |
+-----------------------------+---------+
| | | | |
0 r i b length
We didn't get here.