Overview
I would certainly appreciate suggestions of other extra credit activities (preferably via email).
Can you add office hours?
I don’t really have additional time. I’m sorry. I’ll try email.
What are typical preconditions and postconditions for a procedure that sorts lists? (sort lst less-equal?). You may refer to the return value as sorted.
;;; Preconditions:
;;; * lst is not null (some algorithms assume at least one element)
;;; * All the elements of the list have to be amenable to less-equal?
;;; (E.g., if less-equal? is <=, they must all be real; if less-equal?
;;; is string-ci<=?, they must all be strings)
;;; * less-equal? is transitive. That is, if (less-equal? a b) and
;;; (less-equal b c), then (less-equal? a c).
;;; * less-equal? is consistent. That is, if (less-equal? a b) at one
;;; point, (less-equal? a b) holds at all subsequent points.
;;; Postconditions:
;;; * All adjacent pairs are in order.
;;; (less-equal? (list-ref sorted i) (list-ref sorted (+ 1 i)))
;;; * Note: By transivity, each element is less than or equal to
;;; all subsequent elements.
;;; * sorted is a permutation of lst. We neither added nor removed
;;; elements
What’s a non-transitive less-equal??
(define my-comparison?
(lambda (a b)
(zero? (random 2))))
(define favorite?
(lambda (a b)
(or (and (equal? a "first") (equal? b "second"))
(and (equal? a "second") (equal? b "third"))
(and (equal? a "third") (equal? b "first")))))
What are typical preconditions and postconditions for a procedure that sorts vectors? (vector-sort! vec less-equal?). There is no return value.
;;; Preconditions:
;;; * vec is not empty (some algorithms assume at least one element)
;;; * All the elements of the vector have to be amenable to less-equal?
;;; (E.g., if less-equal? is <=, they must all be real; if less-equal?
;;; is string-ci<=?, they must all be strings)
;;; * less-equal? is transitive. That is, if (less-equal? a b) and
;;; (less-equal b c), then (less-equal? a c).
;;; * less-equal? is consistent. That is, if (less-equal? a b) at one
;;; point, (less-equal? a b) holds at all subsequent points.
;;; Postconditions:
;;; * All adjacent pairs are in order.
;;; (less-equal? (vec-ref vec i) (vec-ref vec (+ 1 i)))
;;; * Note: By transivity, each element is less than or equal to
;;; all subsequent elements.
;;; * We have neither added to nor removed elements from the vector
;;; (The vector contains a permutation of its original elements.)
_How would you document the following procedure?_
```drracket
;;; Procedure:
;;; swap!
;;; Parameters:
;;; vec, a vector
;;; a, a non-negative integer
;;; b, a non-negative integer
;;; Purpose:
;;; Swaps the two elements of the vector at indices a and b.
;;; Produces:
;;; [Nothing; called for the side effect]
;;; Preconditions:
;;; * 0 <= a < (vector-length vec)
;;; * 0 <= b < (vector-length vec)
;;; * A = (vector-ref vec a)
;;; * B = (vector-ref vec b)
;;; Postconditions:
;;; * The length of the vector hasn't changed.
;;; * For all i, 0 <= i < (vector-length vec); if i is not a and i is
;;; not b, then (vector-ref vec i) is unchanged.
;;; * (vector-ref vec a) = B
;;; * (vector-ref vec b) = A
;;; * "The elements at positions a and b swapped."
(define swap!
(lambda (vec a b)
(let ([val (vector-ref vec a)])
(vector-set! vec a (vector-ref vec b))
(vector-set! vec b val))))
What is the basic idea of insertion sort?
In vectors, we designate a boundary in the vector
+--------------+-----------------------+
| sorted | unsorted |
+--------------+-----------------------+
Why does vector-insert! take four parameters?
(define vector-insert!
(lambda (vec new-element boundary less-equal?)
...))
vec is the vector we are working withnew-element is the new element we are insertingboundary is the boundary between the sorted section and the
unsorted section.less-equal? is the same thing it’s always been, the criterion by
which we order valuesAn Example
+---+---+---+---+---+---+---+---+
| 7 | 1 | 4 | 6 | 5 | 0 | 2 | 3 |
+---+---+---+---+---+---+---+---+
|
+---+---+---+---+---+---+---+---+
| 1 | 7 | 4 | 6 | 5 | 0 | 2 | 3 |
+---+---+---+---+---+---+---+---+
|
+---+---+---+---+---+---+---+---+
| 1 | 4 | 7 | 6 | 5 | 0 | 2 | 3 |
+---+---+---+---+---+---+---+---+
|
+---+---+---+---+---+---+---+---+
| 1 | 4 | 6 | 7 | 5 | 0 | 2 | 3 |
+---+---+---+---+---+---+---+---+
|
+---+---+---+---+---+---+---+---+
| 1 | 4 | 5 | 6 | 7 | 0 | 2 | 3 |
+---+---+---+---+---+---+---+---+
|