[an error occurred while processing this directive]

Class 11 Notes

Lecture Slides, Suitable For Printing

Sorting Half and Half

(define (sublist lst start end)
  (if (= start 0)
      (if (= end 0)
          null
          (cons (car lst)
                (sublist (cdr lst) start (- end 1))))
      (sublist (cdr lst) (- start 1) (- end 1))))

(define (first-half lst)
  (sublist lst 0  (floor (/ (+ 1 (length lst)) 2))))

(define (second-half lst)
  (sublist lst (floor (/ (+ 1 (length lst)) 2)) (length lst)))

(define (insert-one el lst cf)
  (if (null? lst) 
      (list el)
      (if (null? (cdr lst))
          (if (cf el (car lst))
              (cons el lst)
              (list (car lst) el))
          (let ((front (first-half lst))
                (back (second-half lst)))     
            (if (cf el (car back))
                (append (insert-one el front cf) back)                        
                (append front (insert-one el back cf)))))))

(define (insert-sort lst cf)
  (if (null? lst) null
      (insert-one (car lst) (insert-sort (cdr lst) cf) cf)))

Logarithms

The logarithm base b (logb) of a number n is the number x such that:
   bx = n

Changing bases: logbn = (1/logkb) logkn

If doubling the size of the input (n) increases the work required by a constant amount, then the running time is in Θ(log n). (Note that although the log is base two because it is doubling, the actual base of the log is not needed within a Θ operator, since it only changes the value by a constant factor.)

Sorting with Binary Trees

(define (make-tree left element right)
  (cons element (cons left right)))

(define (tree-element tree)
  (car tree))

(define (tree-left tree)
  (car (cdr tree)))

(define (tree-right tree)
  (cdr (cdr tree)))

(define (insert-one-tree cf el tree)
  (if (null? tree)
      (make-tree null el null)
      (if (cf el (tree-element tree))
          (make-tree
           (insert-one-tree cf el 
                            (tree-left tree))
           (tree-element tree)
           (tree-right tree))
          (make-tree
           (tree-left tree)
           (tree-element tree)
           (insert-one-tree cf el 
                            (tree-right tree))))))

(define (extract-elements tree)
  (if (null? tree) null
      (append 
       (extract-elements (tree-left tree))
       (cons (tree-element tree)
             (extract-elements (tree-right tree))))))

(define (insert-sort-tree lst cf)
  (define (insert-sort-helper lst cf)
    (if (null? lst) null
        (insert-one-tree 
         cf (car lst) 
         (insert-sort-helper (cdr lst) cf))))
  (extract-elements 
   (insert-sort-helper lst cf)))
Sir Tony Hoare developed the QuickSort algorithm in 1960. His first assignment at a job with a small computer manufacturer was to implement a sorting routine based on the best then-known algorithm (Shell Sort, which is a Θ(n2) algorithm that is a slight improvement on the insert-sort alrogithm we saw in class). He thought he could do better, and his boss bet him sixpence that he couldn't. Quicksort was the algorithm that resulted. Since it is Θ(n log n) on average, it is clearly better than Shell Sort theoretically (that is, it is always eventually faster once n is large enough), and is also better than Shell Sort in practice for nearly all applications.

Hoare received the 1980 Turing Award for contributions to the design of programming languages. The Turing Award is the highest award given in Computer Science, it is named for Alan Turing, one of the codebreakers who worked at Bletchley Park during WWII. We will learn about one of Turing's contributions to cryptography in Friday's class (and on Exam 1). We will learn about Turing's most fundamental contributions to Computer Science after Spring Break.

In his award speech, The Emperor's Old Clothes, Tony Hoare presented principles from his experiences designing programming languages. One of his claims is,

I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.
CS150 students would be wise to follow this advice in all your programs!

[an error occurred while processing this directive]