| 1 | ;;; The sort package -- vector heap sort -*- Scheme -*- |
| 2 | ;;; Copyright (c) 2002 by Olin Shivers. |
| 3 | ;;; This code is open-source; see the end of the file for porting and |
| 4 | ;;; more copyright information. |
| 5 | ;;; Olin Shivers 10/98. |
| 6 | |
| 7 | ;;; Exports: |
| 8 | ;;; (vector-heap-sort! elt< v [start end]) -> unspecified |
| 9 | ;;; (vector-heap-sort elt< v [start end]) -> vector |
| 10 | |
| 11 | ;;; Two key facts |
| 12 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 13 | ;;; If a heap structure is embedded into a vector at indices [start,end), then: |
| 14 | ;;; 1. The two children of index k are start + 2*(k-start) + 1 = k*2-start+1 |
| 15 | ;;; and start + 2*(k-start) + 2 = k*2-start+2. |
| 16 | ;;; |
| 17 | ;;; 2. The first index of a leaf node in the range [start,end) is |
| 18 | ;;; first-leaf = floor[(start+end)/2] |
| 19 | ;;; (You can deduce this from fact #1 above.) |
| 20 | ;;; Any index before FIRST-LEAF is an internal node. |
| 21 | |
| 22 | (define (really-vector-heap-sort! elt< v start end) |
| 23 | ;; Vector V contains a heap at indices [START,END). The heap is in heap |
| 24 | ;; order in the range (I,END) -- i.e., every element in this range is >= |
| 25 | ;; its children. Bubble HEAP[I] down into the heap to impose heap order on |
| 26 | ;; the range [I,END). |
| 27 | (define (restore-heap! end i) |
| 28 | (let* ((vi (vector-ref v i)) |
| 29 | (first-leaf (quotient (+ start end) 2)) ; Can fixnum overflow. |
| 30 | (final-k (let lp ((k i)) |
| 31 | (if (>= k first-leaf) |
| 32 | k ; Leaf, so done. |
| 33 | (let* ((k*2-start (+ k (- k start))) ; Don't overflow. |
| 34 | (child1 (+ 1 k*2-start)) |
| 35 | (child2 (+ 2 k*2-start)) |
| 36 | (child1-val (vector-ref v child1))) |
| 37 | (call-with-values |
| 38 | (lambda () |
| 39 | (if (< child2 end) |
| 40 | (let ((child2-val (vector-ref v child2))) |
| 41 | (if (elt< child2-val child1-val) |
| 42 | (values child1 child1-val) |
| 43 | (values child2 child2-val))) |
| 44 | (values child1 child1-val))) |
| 45 | (lambda (max-child max-child-val) |
| 46 | (cond ((elt< vi max-child-val) |
| 47 | (vector-set! v k max-child-val) |
| 48 | (lp max-child)) |
| 49 | (else k))))))))) ; Done. |
| 50 | (vector-set! v final-k vi))) |
| 51 | |
| 52 | ;; Put the unsorted subvector V[start,end) into heap order. |
| 53 | (let ((first-leaf (quotient (+ start end) 2))) ; Can fixnum overflow. |
| 54 | (do ((i (- first-leaf 1) (- i 1))) |
| 55 | ((< i start)) |
| 56 | (restore-heap! end i))) |
| 57 | |
| 58 | (do ((i (- end 1) (- i 1))) |
| 59 | ((<= i start)) |
| 60 | (let ((top (vector-ref v start))) |
| 61 | (vector-set! v start (vector-ref v i)) |
| 62 | (vector-set! v i top) |
| 63 | (restore-heap! i start)))) |
| 64 | |
| 65 | ;;; Here are the two exported interfaces. |
| 66 | |
| 67 | (define (vector-heap-sort! elt< v . maybe-start+end) |
| 68 | (call-with-values |
| 69 | (lambda () (vector-start+end v maybe-start+end)) |
| 70 | (lambda (start end) |
| 71 | (really-vector-heap-sort! elt< v start end)))) |
| 72 | |
| 73 | (define (vector-heap-sort elt< v . maybe-start+end) |
| 74 | (call-with-values |
| 75 | (lambda () (vector-start+end v maybe-start+end)) |
| 76 | (lambda (start end) |
| 77 | (let ((ans (vector-portion-copy v start end))) |
| 78 | (really-vector-heap-sort! elt< ans 0 (- end start)) |
| 79 | ans)))) |
| 80 | |
| 81 | ;;; Notes on porting |
| 82 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 83 | ;;; |
| 84 | ;;; Bumming the code for speed |
| 85 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 86 | ;;; If you can use a module system to lock up the internal function |
| 87 | ;;; REALLY-VECTOR-HEAP-SORT! so that it can only be called from VECTOR-HEAP-SORT and |
| 88 | ;;; VECTOR-HEAP-SORT!, then you can hack the internal functions to run with no safety |
| 89 | ;;; checks. The safety checks performed by the exported functions VECTOR-HEAP-SORT & |
| 90 | ;;; VECTOR-HEAP-SORT! guarantee that there will be no type errors or array-indexing |
| 91 | ;;; errors. In addition, with the exception of the two computations of |
| 92 | ;;; FIRST-LEAF, all arithmetic will be fixnum arithmetic that never overflows |
| 93 | ;;; into bignums, assuming your Scheme provides that you can't allocate an |
| 94 | ;;; array so large you might need a bignum to index an element, which is |
| 95 | ;;; definitely the case for every implementation with which I am familiar. |
| 96 | ;;; |
| 97 | ;;; If you want to code up the first-leaf = (quotient (+ s e) 2) computation |
| 98 | ;;; so that it will never fixnum overflow when S & E are fixnums, you can do |
| 99 | ;;; it this way: |
| 100 | ;;; - compute floor(e/2), which throws away e's low-order bit. |
| 101 | ;;; - add e's low-order bit to s, and divide that by two: |
| 102 | ;;; floor[(s + e mod 2) / 2] |
| 103 | ;;; - add these two parts together. |
| 104 | ;;; giving you |
| 105 | ;;; (+ (quotient e 2) |
| 106 | ;;; (quotient (+ s (modulo e 2)) 2)) |
| 107 | ;;; If we know that e & s are fixnums, and that 0 <= s <= e, then this |
| 108 | ;;; can only fixnum-overflow when s = e = max-fixnum. Note that the |
| 109 | ;;; two divides and one modulo op can be done very quickly with two |
| 110 | ;;; right-shifts and a bitwise and. |
| 111 | ;;; |
| 112 | ;;; I suspect there has never been a heapsort written in the history of |
| 113 | ;;; the world in C that got this detail right. |
| 114 | ;;; |
| 115 | ;;; If your Scheme has a faster mechanism for handling optional arguments |
| 116 | ;;; (e.g., Chez), you should definitely port over to it. Note that argument |
| 117 | ;;; defaulting and error-checking are interleaved -- you don't have to |
| 118 | ;;; error-check defaulted START/END args to see if they are fixnums that are |
| 119 | ;;; legal vector indices for the corresponding vector, etc. |