Algorithms and OOD (CSC 207 2014S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions] [GNU Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013F (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Admin
Why don't you like syntax coloring?
Distracting. Sometimes confusing.
Talk about good code design
Process concepts:
for (int i = 0; i < str.length(); i++) { this.contents[start+i] = str.toCharArray()[i]; } // for
Problems
A better version
int strlen = str.length(); char[] strchars = str.toCharArray(); for (int i = 0; i < strlen; i++) { this.contents[start+i] = strchars[i]; } / for
But, doesn't show knowledge of the library. E.g., String.charAt, which we could assume is constant time.
int strlen = str.length(); for (int i = 0; i < strlen; i++) { this.contents[start+i] = str.charAt(i); } / for
But there's more to the library. System.arraycopy is intended to do quick copying of values.
System.arraycopy(str.toCharArray(), 0, this.contents, start, str.length());
The result is shorter (in terms of code we write), more likely to be correct, likely to be faster, and potentially more readable.
Problem: Insert an array into the middle of another array
First model: Three loops (or three calls to System.arraycopy)
// Deal with the stuff before the insertion point
// Deal with the stuff after the insertion point
// Deal with the inserted stuff
Second model: One loop, asking where we are at each point
for each position in the target array
if we're before the insertion point ...
if we're in the insertion section
if we're after the insertion section
In case it's not clear, Sam would suggest the first.
// Copy the characters from the replacment // Shift the remaining stuff over (vague)
Although we say the constant multipler doesn't really matter, it makes a HUGE difference for O(log_2(n)) algorithms.
Suppose we have an O(log_2(n)) algorithm that can process a million-item collection in a minute
How big a collection can an algorithm that is twice as fast process in a minute? Now, we can do billion-element collection.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions] [GNU Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013F (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Copyright (c) 2013-14 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.