This preview shows pages 1–3. Sign up to view the full content.
11/8/2009
1
Memory Models
EECS 280
Programming and Introductory Data Structures
1
Invariants
Recall:
Checking for Representation Invariants
2
c
Last time, we showed how to confirm that an array was
sorted, with no duplicates.
c
Base cases:
c
An array of zero elements is sorted and has no duplicates
c
An array of one element is sorted and has no duplicates
c
Inductive step:
An array of N elements (N>=2) is sorted and has no
duplicates if:
c
A[0] < A[1]
c
The array A[1].
..A[N1] is sorted and has no duplicates.
Invariants
Checking for Representation Invariants
3
bool strictSorted(int a[], int size)
{
// REQUIRES: a has size elements
// EFFECTS: returns true if a is sorted
//
with no duplicates
if (size <= 1) return true;
// base cases
for (i=0; i<size1; i++){ // iterative step
if (a[i] >= a[i+1]) {
return false;
}
}
return true;
}
Invariants
Checking for Representation Invariants (Continued)
4
c
If the array is unsorted, we only have to find duplicates.
But, this
is actually harder.
c
Base case:
c
An array with 0 elements has no duplicates.
c
Inductive Step:
An array with N elements (N>0) has no duplicates if:
c
A[0] doesn't appear anywhere else in the array.
c
The remainder of the array has no duplicates.
c
The basic strategy is, for each element, see if there are any
elements to the "right" that are equal.
c
To do this, we'll have to write a "nested loop".
Invariants
Checking for Representation Invariants (Continued)
5
bool noDuplicates(int a[], int size) {
for (int i = 0; i < size; i++) {
// Check elements to the right of i
}
}
i
is the "outer loop index“.
It points to
the element currently under consideration.
The "outer loop" is the one that considers
each element of the array.
Invariants
Checking for Representation Invariants (Continued)
6
bool noDuplicates(int a[], int size) {
for (int i = 0; i < size; i++) {
// Check elements to the right of i
}
}
c
For each element to the right, we need to check if it is a
duplicate of
i
:
i
Check all in here
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 11/8/2009
2
Invariants
Checking for Representation Invariants (Continued)
7
bool noDuplicates(int a[], int size) {
for (int i = 0; i < size; i++) {
// Check elements to the right of i
}
}
c
For each element to the right, we need to check if it is a
duplicate of
i
:
i
Check all in here
We need a new loop index (
j
), which starts at
i+1
and goes until
size1
.
Note that this interval
might be empty, and it will be if
i==size
.
Invariants
Checking for Representation Invariants (Continued)
8
bool noDuplicates(int a[], int size) {
for (int i=0; i<size; i++) {
for (int j=i+1; j<size; j++) {
if (a[i] == a[j]) return false;
}
}
return true;
}
If these two elements are equal, there are
duplicates, so return false.
If we fall through the loop, there are no
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.
 Fall '08
 NOBLE

Click to edit the document details