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
Iterative:
(define insertion-sort (lambda (lst) (let loop ([remaining lst] [sorted null]) (if (null? remaining) sorted (loop (cdr remaining) (insert (car remaining) (sorted)))))))
Analysis
Recursive functions:
t(n) = c + t(n/2) t(1) = d t(2) = c + t(2/2) = c + t(1) = c + d t(4) = c + t(4/2) = c + t(2) = c + c + d = 2c + d t(8) = c + t(8/2) = c + t(4) = c + 2c + d = 3c + d t(2^4) = t(16) = c + t(8) = c + 3c+d = 4c + d
pattern: t(2^k) = k*c + d
t(n) = c + t(n/2) = c + c + t(n/4) = 2c + t(n/4) = 2c + c + t(n/8) = 3c + t(n/8) = 3c + c + t(n/16) = 4c + t(n/16)
pattern: t(n) = kc + t(n/2^k) When n = 2^k, this is t(n) = kc + t(1) = kc + d When n = 2^k, k = log2(n) So t(n) = log2(n)c + d is in O(log_2(n))
When you write a searching or sorting algorithm, you often want a function as a parameter
;;; Find the first ok thing in the list
(define search
(lambda (lst ok?)
(if (null? lst)
#f
(if (ok? (car lst))
(car lst)
(search (cdr lst) ok?)))))
Java does not (currently) allow functions as first class values. But it does allow objects/interfaces as first-class values. Instead of passing in a function, we pass in an object that contains that function/method.
public interface Predicate
{
public boolean ok(Object o);
} // interface Predicate
public class LessThanTwo
implements Predicate
{
public boolean ok(Object o)
{
return (o instanceof Number) &&
(((Number) o).doubleValue < 2.0);
} // ok
} // class LessThanTwo
Great idea in Scheme: Anonymous functions
(search students (lambda (student) (and (here? student) (awake? student))))
Java has anonymous classes! We'll look at them later.
What's the difference between Java's Comparator and Comparable?
Comparator<T>
)See forthcoming reading.
What's wrong with the following?
if (vals[mid] > val)
vals = Arrays.copyOfRange(vals, 0, mid);
else if (vals[mid] < val)
vals = Arrays.copyOfRange(vals, mid+1, vals.length);
int mid = lower + upper/2;
int mid = (lower + upper)/2;
I'm proud of you.
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.