fork download
  1. (defun bubble-sort (sequence predicate)
  2. (loop with seq = (copy-seq sequence)
  3. with len = (length seq)
  4. with d = 1
  5. for i to (- len 2)
  6. thereis (and (zerop (loop for j to (- len d i 1)
  7. unless (or (equalp (elt seq j) (elt seq (+ j d)))
  8. (funcall predicate (elt seq j) (elt seq (+ j d))))
  9. do (rotatef (elt seq j) (elt seq (+ j d)))
  10. and count t))
  11. seq)
  12. finally (return seq)))
  13.  
  14. (defun comb-sort (sequence predicate)
  15. (loop with seq = (copy-seq sequence)
  16. with len = (length seq)
  17. for d = (floor (/ len 1.3)) then (if (eq d 1) d (floor (/ d 1.3)))
  18. thereis (and (zerop (loop for j to (- len d 1)
  19. unless (or (equalp (elt seq j) (elt seq (+ j d)))
  20. (funcall predicate (elt seq j) (elt seq (+ j d))))
  21. do (rotatef (elt seq j) (elt seq (+ j d)))
  22. and count t))
  23. (eq d 1)
  24. seq)))
  25.  
  26. (loop with seq = (loop repeat 1000 collect (random 1000))
  27. for sort in '(bubble-sort comb-sort)
  28. do
  29. (format t "~a~%" sort)
  30. (time (funcall (symbol-function sort) seq #'<))
  31. (format t "~%"))
Success #stdin #stdout 4.5s 10616KB
stdin
Standard input is empty
stdout
BUBBLE-SORT
Real time: 4.310393 sec.
Run time: 4.305346 sec.
Space: 8000 Bytes
COMB-SORT
Real time: 0.164153 sec.
Run time: 0.163975 sec.
Space: 9392 Bytes