Algorithms and OOD (CSC 207 2013F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Overview
Why might the following upset me?
int indexOfSmallest(T[] vals, Comparator<T> order, int lb, int ub) {
Integer index;
Integer minIndex = lb;
for ( index = lb + 1; index < ub; index++){
if ( order.compare(vals[minIndex], vals[index]) > 0){
minIndex = index;
}
}
return minIndex;
} // indexOfSmallest(T[], Comparator<T>, int, int)
What should happen to a 161 student who writes the following at the end of the semester?
char *str = (char *) malloc(12 * sizeof(char));
str = "Hello world";
Important lesson: In C, when you assign to a pointer, you change what it points to.
Related lesson: In Java, when you assign to an object, you're just assigning a pointer (although we call it a reference).
String a = "Hello"; String b = "Goodbye"; String c = a; a = b;
What's the relation to that and the following Java?
T[] values = (T[]) new Object[computedSize];
values = merge(vals1, lb1, ub1, vals2, lb2, ub2, order);
What's wrong with the following code?
int indexOfSmallest(T[] vals, Comparator<T> order, int lb, int ub) {
int i = 0; // Fix i = lb
while(i<vals.length){
if (order.compare(vals[i], vals[lb]) < 0) { // Fix i = ub
lb = i;
}
i++;
}
return lb;
} // indexOfSmallest(T[], Comparator<T>, int, int)
What happens if we use this for selection sort?
5, 1, 6, 2, 8, 3
*
1, 5, 6, 2, 8, 3
! *
5, 1, 6, 2, 8, 3
! ! *
The code we develop can be found in examples/sorting-pause
The code we develop can be found in examples/sorting-pause
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (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 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.