home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / s48.zip / MISC / SORT.SCM < prev    next >
Text File  |  1990-07-17  |  6KB  |  152 lines

  1. ;;; Copyright (c) 1985 Yale University
  2. ;;;     Authors: N Adams, R Kelsey, D Kranz, J Philbin, J Rees.
  3.  
  4. ;;; This material was developed by the T Project at the Yale
  5. ;;; University Computer Science Department.  Permission to copy this
  6. ;;; software, to redistribute it, and to use it for any purpose is
  7. ;;; granted, subject to the following restric- tions and
  8. ;;; understandings.
  9. ;;; 1. Any copy made of this software must include this copyright
  10. ;;;    notice in full.
  11. ;;; 2. Users of this software agree to make their best efforts (a) to return
  12. ;;;    to the T Project at Yale any improvements or extensions that they make,
  13. ;;;    so that these may be included in future releases; and (b) to inform
  14. ;;;    the T Project of noteworthy uses of this software.
  15. ;;; 3. All materials developed as a consequence of the use of this software
  16. ;;;    shall duly acknowledge such use, in accordance with the usual standards
  17. ;;;    of acknowledging credit in academic research.
  18. ;;; 4. Yale has made no warrantee or representation that the operation of
  19. ;;;    this software will be error-free, and Yale is under no obligation to
  20. ;;;    provide any services, by way of maintenance, update, or otherwise.
  21. ;;; 5. In conjunction with products arising from the use of this material,
  22. ;;;    there shall be no use of the name of the Yale University nor of any
  23. ;;;    adaptation thereof in any advertising, promotional, or sales literature
  24. ;;;    without prior written consent from Yale in each case.
  25. ;;;
  26.  
  27. ;;; We gratefully acknowledge Bob Nix
  28.  
  29. ;;;  SORT:ONLINE-MERGE-SORT!
  30. ;;;  =======================
  31. ;;;  On-Line Merge sort, a fast and stable algorithm for sorting a list.
  32. ;;;  This is a very neat algorithm!  Consider the following code:
  33. ;;;
  34. ;;;    (DEFINE (MERGE-SORT L)
  35. ;;;        (IF (NULL? (CDR L))
  36. ;;;            L
  37. ;;;            (MERGE (MERGE-SORT (FIRST-HALF-OF L))
  38. ;;;                   (MERGE-SORT (SECOND-HALF-OF L)))))
  39. ;;;
  40. ;;;  The nested calls to MERGE above form a binary tree, with MERGE's of
  41. ;;;  singleton lists at the leaves, and a MERGE of two lists of size N/2 at
  42. ;;;  the top.  The algorithm below traverses this MERGE-tree in post-order,
  43. ;;;  moving from the lower left hand corner to the right.
  44. ;;;
  45. ;;;  This algorithm sorts N objects with about NlgN+2N comparisons and exactly
  46. ;;;  lgN conses.  The algorithm used is a version of mergesort that is
  47. ;;;  amenable to Lisp's data accessing primitives.  The first phase of the
  48. ;;;  algorithm is an "addition" phase in which each element X is added to
  49. ;;;  a list of lists of sorted runs B in much the same manner as a one is
  50. ;;;  added to a binary number.  If the first "digit" of B is 0, i.e. the first
  51. ;;;  list in B is NIL, then the element to be added becomes the first digit
  52. ;;;  of B.  If that digit is non empty then you merge the digit with X and
  53. ;;;  recurse on the rest of B -- setting the first digit of B to be zero.
  54. ;;;  For example:
  55. ;;;
  56. ;;;   Reversed      LIST B
  57. ;;;   Binary #     Each sublist is sorted.
  58. ;;;
  59. ;;;    0000        ()
  60. ;;;    1000        ((x))
  61. ;;;    0100        (()  (x x))
  62. ;;;    1100        ((x) (x x))
  63. ;;;    0010        (()  ()    (x x x x))
  64. ;;;    1010        ((x) ()    (x x x x))
  65. ;;;    0110        (()  (x x) (x x x x))
  66. ;;;    1110        ((x) (x x) (x x x x))
  67. ;;;    0001        (()  ()    ()        (x x x x x x x x))
  68. ;;;    1001        ((x) ()    ()        (x x x x x x x x))
  69. ;;;
  70. ;;;  The algorithm then merges the sublists of these lists into
  71. ;;;  one list, and returns that list.
  72. ;;;
  73. ;;;  To see the algorithm in action, trace LIST-MERGE!.
  74. ;;;
  75.  
  76. ;;; Returns list L sorted using OBJ-< for comparisons.
  77.  
  78. (define (sort-list l obj-<)
  79.   (cond ((or (null? l)
  80.              (null? (cdr l)))
  81.          l)
  82.         (else
  83.          (online-merge-sort! (append l '())    ; copy-list
  84.                  obj-<))))
  85.  
  86. ;;; Returns list L sorted using OBJ-< for comparisons.
  87. ;;; L is destructively altered.
  88.  
  89. (define (sort-list! l obj-<)
  90.   (cond ((or (null? l)
  91.              (null? (cdr l)))
  92.          l)
  93.         (else
  94.          (online-merge-sort! l obj-<))))
  95.  
  96. ;;; The real sort procedure.  Elements of L are added to B, a list of sorted
  97. ;;; lists as defined above.  When all elements of L have been added to B
  98. ;;; the sublists of B are merged together to get the desired sorted list.
  99.  
  100. (define (online-merge-sort! l obj-<)
  101.   (let ((b (cons '() '())))
  102.     (let loop ((l l))
  103.       (cond ((null? l)
  104.              (do ((c (cddr b) (cdr c))
  105.                   (r (cadr b) (list-merge! (car c) r obj-<)))
  106.                  ((null? c)
  107.                   r)))
  108.             (else
  109.              (let ((new-l (cdr l)))
  110.                (set-cdr! l '())
  111.                (add-to-sorted-lists l b obj-<)
  112.                (loop new-l)))))))
  113.  
  114. ;;; X is a list that is merged into B, the list of sorted lists.
  115.  
  116. (define (add-to-sorted-lists x b obj-<)
  117.   (let loop ((x x) (b b))
  118.     (let ((l (cdr b)))
  119.       (cond ((null? l)
  120.              (set-cdr! b (cons x '())))
  121.             ((null? (car l))
  122.              (set-car! l x))
  123.             (else
  124.              (let ((y (list-merge! x (car l) obj-<)))
  125.                 (set-car! l '())
  126.                 (loop y l)))))))
  127.  
  128. ;;; Does a stable side-effecting merge of L1 and L2.
  129.  
  130. (define (list-merge! l1 l2 obj-<)
  131.   (cond ((null? l1) l2)
  132.         ((null? l2) l1)
  133.         ((obj-< (car l1) (car l2))
  134.          (real-list-merge! l2 (cdr l1) obj-< l1)
  135.          l1)
  136.         (else
  137.          (real-list-merge! l1 (cdr l2) obj-< l2)
  138.          l2)))
  139.  
  140. ;;; Does the real work of LIST-MERGE!.  L1 is assumed to be non-empty.
  141.  
  142. (define (real-list-merge! l1 l2 obj-< prev)
  143.   (let loop ((a l1) (b l2) (prev prev))
  144.     (cond ((null? b)
  145.            (set-cdr! prev a))
  146.           ((obj-< (car a) (car b))
  147.            (set-cdr! prev a)
  148.            (loop b (cdr a) a))
  149.           (else
  150.            (set-cdr! prev b)
  151.            (loop a (cdr b) b)))))
  152.