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
merge
.merge
Remind me to end by 10:25
Background:
A picture of the state of our system
a1:
+------------+-----------+-------------+------------+
| irrelevant | processed | unprocessed | irrelevant |
+------------+-----------+-------------+------------+
0 lb1 c1 ub1 a1.length
a2:
+------------+-----------+-------------+------------+
| irrelevant | processed | unprocessed | irrelevant |
+------------+-----------+-------------+------------+
0 lb2 c2 ub2 a1.length
target:
+---------------------------+-------------------------+
| processed | "empty" |
+---------------------------+-------------------------+
0 i target.length
Our focus should probably be the processed values in target. What can we say about that section? What should we say? (We'll work both formally and informally.)
Is that enough? What's the basic step we expect to do?
Action: Copy the smaller of a1[c1] and a2[c2] to target[i]
if (order.compare(a1[c1], a2[c2]) <= 0) {
target[i++] = a1[c1++];
} else {
target[i++] = a2[c2++];
} // else: a2[c2] is smaller
When do we stop? When we run out of elements in a1 or a2
a1:
+------------+-----------+-------------+
| irrelevant | processed | irrelevant |
+------------+-----------+-------------+
0 lb1 c1,ub1 a1.length
a2:
+------------+-----------+-------------+------------+
| irrelevant | processed | unprocessed | irrelevant |
+------------+-----------+-------------+------------+
0 lb2 c2 ub2 a1.length
target:
+---------------------------+-------------------------+
| processed | "empty" |
+---------------------------+-------------------------+
0 i target.length
Whoops! Haven't quite achieved the postcondition.
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.