(list 11 8 14 7) split: (list 8 7) 11 (list 14) sort each list: (list 7 8) 11 (list 14) juxtapose them: (list 7 8 11 14)
What's the basic idea?
Here is the complete picture:
| (list 11 8 14 7) | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| (list 7 8 11 14) | |||||||||||||||||||||||||||||
Okay let's try a larger example by hand:
(list 33 1 9 88 77 21 20 19 87 86 99 0)Figure out on your own what's going on.
;; quick-sort : (listof number) -> (listof number)
;; Purpose: sort a list of numbers
(define (quick-sort alon)
(cond
((empty? alon) empty)
(else (append
(quick-sort (smaller-items alon (first alon)))
(list (first alon))
(quick-sort (larger-items alon (first alon)))))))
;; smaller-items : number (listof number) -> (listof number)
;; Purpose: filter out that list of numbers from alon that are smaller than th
(define (smaller-items th alon) ...)
;; larger-items : number (listof number) -> (listof number)
;; Purpose: filter out that list of numbers from alon that are larger than th
(define (larger-items th alon) ...)
The last two are just like demands-going-up and demands-going-down
What's different about this program? How does it differ from, say, insertion sort?
![]() |
![]() |
![]() |
And here is yet another example of this kind:
![]() |
![]() |
The basic idea:
Here we go:
;; sierpinski : posn posn posn -> #t
;; Purpose: draw a sierpinski triangle down to a certain size
(define (sierpinski a b c)
(cond
((too-small? a b) #t)
(else
(local ((define a-b (mid-point a b))
(define b-c (mid-point b c))
(define c-a (mid-point a c)))
(and
(draw-triangle a b c)
(sierpinski a a-b c-a)
(sierpinski b a-b b-c)
(sierpinski c c-a b-c))))))
;; draw-triangle : posn posn posn -> #t
;; Purpose: draw the triangle determined
(define (draw-triangle A B C) ...)
| Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |