13 |
18 |
12 |
11 |
15 |
24 |
23 |
30 |
13 |
18 |
12 |
11 |
15 |
24 |
23 |
30 |
12 |
13 |
18 |
11 |
15 |
24 |
23 |
30 |
11 |
12 |
13 |
18 |
15 |
24 |
23 |
30 |
|
|
|
|
|
|
|
|
a. Label each of the following as PRE-condition, POST-condition, BOTH, or NEITHER. The above spec is informal; make reasonable assumptions based on what you know about sorting and about this algorithm in particular (hint: this could be a helper method for Insertion Sort).
1. A[0..size-2] is sorted
2. A[0..size-1] is sorted
3. A[0..size] is sorted
4. A[0..size+1] is sorted
5. if x is one of the values in A[0..size-1] before the method was called, then x is one of the values in A[0..size] after the method is called.
6. if y is one of the values in A[0..size] after the
method was called, then y was one of the values in A[0..size-1] before the
method was called.
b. Now, study the following partly completed method. Then fill
in the blanks so the method correctly implements the above specification.
Make no other changes.
void insertInteger(int newVal) {
int currentPos;
currentPos=______________;
while (currentPos >= 0 && A[currentPos]
___ newVal)
{
A[currentPos+1]=A[currentPos];
currentPos--;
}
A[________________]=newVal;
size++;
}