Algorithms and OOD (CSC 207 2013F) : EBoards

CSC207.01 2013F, Class 38: Detour: Anonymous Inner Classes


Overview

Preliminaries

Admin

Questions on HW9

Detour: An invariant for 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.

Anonymous inner classes

Lab

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

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.