17+-+Memory+Models - 9/1/10 Invariants Recall: Checking for...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
9/1/10 1 Memory Models EECS 280 Programming and Introductory Data Structures 1 Invariants Recall: Checking for Representation Invariants 2 Last time, we showed how to confirm that an array was sorted, with no duplicates. Base cases: An array of zero elements is sorted and has no duplicates An array of one element is sorted and has no duplicates Inductive step: An array of N elements (N>=2) is sorted and has no duplicates if: A[0] < A[1] The array A[1]. ..A[N-1] 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<size-1; i++){ // iterative step if (a[i] >= a[i+1]) { return false; } } return true; } Invariants Checking for Representation Invariants (Continued) 4 If the array is unsorted, we only have to find duplicates. But, this is actually harder. Base case: An array with 0 elements has no duplicates. Inductive Step: An array with N elements (N>0) has no duplicates if: A[0] doesn't appear anywhere else in the array. The remainder of the array has no duplicates. The basic strategy is, for each element, see if there are any elements to the "right" that are equal. 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 } } For each element to the right, we need to check if it is a duplicate of i : i Check all in here
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9/1/10 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 } } 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 size-1 . 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

17+-+Memory+Models - 9/1/10 Invariants Recall: Checking for...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online