8.6.0.14

Lecture 22: Merge sort

This assignment is due on Tuesday, November 1 at 11:59pm. Submit it using Handin as assignment lecture22.

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. Recall insertion sort from Lecture 19: Follow the template. 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.