8.16

Lecture 23: Merge sort🔗

This assignment is due on Sunday, April 6 at 11:59pm. Submit it using Handin as assignment lecture23.

Exercise 1. How does merge 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:
    ; Merge sort:
    ; <bpmn:definitions ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... </bpmn:definitions>

; A MergeTree is one of:
; - (make-split MergeTree MergeTree)
; - (make-single Number)
(define-struct split [former latter])
(define-struct single [element])
 
(define mt0 (make-single 8))
(define mt1 (make-split mt0 (make-single 2)))
(define mt2 (make-split mt1
                        (make-split (make-single 3)
                                    (make-single 7))))

Exercise 2. Define mt3 to be the MergeTree that decomposes this sorting problem:

 5 6 2 1 8 2 3 7

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

Exercise 4. Find all the recursive calls below. For each recursive call, write a comment that says
  • what the recursive call is

  • and whether it follows the template

  • for processing which data definition.

The first two recursive calls 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 recursive call (sum (rest lon))
; 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 recursive call (generate-merge-tree (take (floor (/ (length lon) 2)) lon))
; does 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)))]))
 
; 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))))]))

Exercise 5. Recall insertion sort from Lecture 20: Insertion sort. How does insertion 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:
    ; Insert sort:
    ; <bpmn:definitions ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... ..... ..... ..... .....
    ; ..... ..... </bpmn:definitions>

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.

Optional: Read Chapters 25–26 of the textbook.