( defun bubble-sort ( sequence predicate)
( loop with seq = ( copy-seq sequence)
with len = ( length seq)
with d = 1
for i to ( - len 2 )
thereis ( and ( zerop ( loop for j to ( - len d i 1 )
unless ( or ( equalp ( elt seq j) ( elt seq ( + j d) ) )
( funcall predicate ( elt seq j) ( elt seq ( + j d) ) ) )
do ( rotatef ( elt seq j) ( elt seq ( + j d) ) )
and count t) )
seq)
finally ( return seq) ) )
( defun comb-sort ( sequence predicate)
( loop with seq = ( copy-seq sequence)
with len = ( length seq)
for d = ( floor ( / len 1.3 ) ) then ( if ( eq d 1 ) d ( floor ( / d 1.3 ) ) )
thereis ( and ( zerop ( loop for j to ( - len d 1 )
unless ( or ( equalp ( elt seq j) ( elt seq ( + j d) ) )
( funcall predicate ( elt seq j) ( elt seq ( + j d) ) ) )
do ( rotatef ( elt seq j) ( elt seq ( + j d) ) )
and count t) )
( eq d 1 )
seq) ) )
( loop with seq = ( loop repeat 1000 collect ( random 1000 ) )
for sort in '( bubble-sort comb-sort)
do
( format t "~a~%" sort)
( time ( funcall ( symbol-function sort) seq #'< ) )
( format t "~%" ) )
KGRlZnVuIGJ1YmJsZS1zb3J0IChzZXF1ZW5jZSBwcmVkaWNhdGUpCiAgKGxvb3Agd2l0aCBzZXEgPSAoY29weS1zZXEgc2VxdWVuY2UpCgkJd2l0aCBsZW4gPSAobGVuZ3RoIHNlcSkKCQl3aXRoIGQgPSAxCgkJZm9yIGkgdG8gKC0gbGVuIDIpCgkJdGhlcmVpcyAoYW5kICh6ZXJvcCAobG9vcCBmb3IgaiB0byAoLSBsZW4gZCBpIDEpCgkJCQkJCQkJICB1bmxlc3MgKG9yIChlcXVhbHAgKGVsdCBzZXEgaikgKGVsdCBzZXEgKCsgaiBkKSkpCgkJCQkJCQkJCQkJIChmdW5jYWxsIHByZWRpY2F0ZSAoZWx0IHNlcSBqKSAoZWx0IHNlcSAoKyBqIGQpKSkpCgkJCQkJCQkJICBkbyAocm90YXRlZiAoZWx0IHNlcSBqKSAoZWx0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