8.5

Lecture 23: Quick sort

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

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?)