8.16

Lecture 24: Quick sort🔗

This assignment is due on Tuesday, April 8 at 11:59pm. Submit it using Handin as assignment lecture24.

Exercise 1. How does quick sort decompose this sorting problem into smaller sorting problems?

 5 6 2 1 8 2 3 7

  1. Use this app to draw the decomposition. Make a rectangle for every problem.

  2. Click the “Copy to clipboard” button in the lower-left corner of the app.

  3. Paste the code as a comment, like this:
    ; Quick sort:
    ; <bpmn:definitions ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... </bpmn:definitions>

; A PivotTree is one of:
; - (make-no-pivot)
; - (make-pivot PivotTree Number PivotTree)
(define-struct no-pivot [])
(define-struct pivot [left val right])
 
(define pt0 (make-no-pivot))
(define pt1 (make-pivot pt0 1 (make-no-pivot)))
(define pt2 (make-pivot pt1 2 (make-pivot (make-no-pivot) 3 (make-no-pivot))))

Exercise 2. Define pt3 to be the PivotTree that decomposes this sorting problem:

 5 6 2 1 8 2 3 7

Exercise 3. Write the template for a function that processes a PivotTree. Make it look like a function called process-pivottree, and do not put it in a comment.

Exercise 4. Design the function larger, which takes a Number and a [ListOf Number] and produces a new list of all the numbers in the list greater than the given number. You can use filter.

Exercise 5. What would happen if smaller used < instead of <=? Write a correct test for the quick-sort function that would detect the problem. In other words, the test should fail if smaller used < instead of <=.

The code written in the videos above is available for your reference. To download it, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.

(Who’s the pivot?)

Exercise 6. For each function definition below, write a comment that says whether it needs a termination argument and why. The first two functions are done for you below, to show you how to format each comment.
; sum : ListOfNumbers -> Number
(define (sum lon)
  (cond [(empty? lon) 0]
        [else (+ (first lon) (sum (rest lon)))]))
 
; The function sum does not need a termination argument,
; because its recursive call follows the template for processing a ListOfNumbers.
 
; generate-merge-tree : [NEListOf Number] -> MergeTree
(define (generate-merge-tree lon)
  (cond [(= 1 (length lon))
         (make-single (first lon))]
        [else (make-split
               (generate-merge-tree (take (floor (/ (length lon) 2)) lon))
               (generate-merge-tree (drop (floor (/ (length lon) 2)) lon)))]))
 
; The function generate-merge-tree needs a termination argument,
; because its recursive calls do not follow the template for processing a [NEListOf Number].
 
; optophone : NaturalNumber -> Image
(define (optophone n)
  (cond [(= n 0) figure]
        [else (overlay (optophone (- n 1))
                       (circle (- (* n 20) 10) "solid" "white")
                       (circle (* n 20) "solid" "black"))]))
 
; binary : NaturalNumber -> String
(define (binary n)
  (cond [(= n 0) "a"]
        [(even? n) (string-append (binary (/ n 2)) "b")]
        [else (string-append (binary (- n 1)) "c")]))
 
; marquee : Number -> Number
(define (marquee n)
  (cond [(< n -9) 10]
        [else (marquee (- n 1))]))
 
; insert-sort : [ListOf Number] -> [ListOf Number]
(define (insert-sort lon)
  (cond [(empty? lon) lon]
        [else (insert (first lon) (insert-sort (rest lon)))]))
 
; insert : Number ListOfNumbers -> ListOfNumbers
(define (insert n lon)
  (cond [(empty? lon) (list n)]
        [(<= n (first lon)) (cons n lon)]
        [else (cons (first lon) (insert n (rest lon)))]))
 
; every-other : [ListOf String] -> [ListOf String]
(define (every-other los)
  (cond [(empty? los) los]
        [(empty? (rest los)) (list (first los))]
        [else (cons (first los) (every-other (rest (rest los))))]))
 
; greet : [ListOf String] -> [ListOf String]
(define (greet los)
  (cond [(empty? los) empty]
        [(cons? los) (cons (string-append "Hello " (first los))
                           (greet (rest los)))]))
 
; ftoc : Number -> Number
(define (ftoc f)
  (/ (- f 32) 1.8))
 
; measure : Posn -> Number
(define (measure p)
  (cond [(= 0 (posn-y p)) (posn-x p)]
        [else (measure (make-posn (posn-y p) (remainder (posn-x p) (posn-y p))))]))
 
; draw-posn : Posn -> Image
(define (draw-posn p)
  (place-image dot (posn-x p) (posn-y p) background))