;;; File: ;;; quicksort.ss ;;; Author: ;;; Samuel A. Rebelsky ;;; Summary: ;;; Procedures for sorting stuff using the legendary Quicksort procedure. ;;; Version: ;;; 1.3 of February 2003 ;;; Contents: ;;; Primary Procedures: ;;; (quicksort lst comes-before? same?) ;;; Sort a list using a variant of the Quicksort procedure. ;;; (quicksort! vec may-precede?) ;;; Sort a vector in place using a variant of the Quicksort procedure. ;;; Primary Helpers: ;;; (select pred? lst) ;;; Select all elements of lst for which pred? holds. ;;; (select-pivot lst) ;;; Select an unpredictable element of lst. ;;; Utility Procedures: ;;; (random-list max len) ;;; Generate a list of length len whose elements are all ;;; between 0 and max, inclusive. ;;; Standard Higher-Order Procedures: ;;; (left-section binproc left) ;;; (right-section binproc right) ;;; History ;;; Thursday, 27 February 2003 [v 1.0] ;;; Created. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Primary Procedures ;;; Procedure: ;;; quicksort ;;; Parameters: ;;; lst, a list to sort ;;; comes-before?, a binary predicate that compares values. ;;; same?, a binary predicate that compares values. ;;; Purpose: ;;; Sort stuff. ;;; Produces: ;;; sorted-stuff, a sorted list ;;; Preconditions: ;;; comes-before? can be applied to any two elements of stuff. ;;; comes-before? represents a transitive operation. ;;; same? can be applied to any two elements of stuff. ;;; For any two values, a and b, exactly one of the following holds: ;;; 1. (comes-before? a b) ;;; 2. (same? a b) ;;; 3. (comes-before? b a) ;;; Postconditions: ;;; sorted-stuff is sorted. That is, any element may precede the ;;; subsequent element. In Scheme, we'd say that ;;; ((disjunction comes-before? same?) (list-ref sorted i) ;;; (list-ref sorted (+ i 1))) ;;; sorted-stuff is a permutation of stuff. ;;; Does not affect stuff. ;;; sorted-stuff may share cons cells with stuff. ;;; Examples: ;;; To sort values, a list of numbers, in increasing order. ;;; (quicksort values < =) ;;; To sort values, a list of numbers, in decreasing order. ;;; (quicksort values > =) ;;; To sort people, a list of (last-name first-name phone-number) ;;; triplets, by first name ;;; (quicksort people ;;; (lambda (person1 person2) ;;; (string 0 ;;; len >= 0 ;;; Postconditions: ;;; The result list has length length. ;;; Every value in the result list is between 0 and max, inclusive. ;;; The result list is hard to predict. (define random-list (lambda (max len) (if (= len 0) null (cons (random (+ max 1)) (random-list max (- len 1)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Standard Higher-Order Procedures (define left-section (lambda (binproc left) (lambda (right) (binproc left right)))) (define right-section (lambda (binproc right) (lambda (left) (binproc left right))))