< Back to IRCAM Forum

Lisp bubble sort function

Hello,

I’m trying to implement a bubble sort in OM 6.12 using some Lisp code I came across here: https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Bubble_sort#Common_Lisp

Here’s the code:

(defun bubble-sort (lst)
(loop repeat (1- (length lst)) do
(loop for ls on lst while (rest ls) do
(when (> (first ls) (second ls))
(rotatef (first ls) (second ls)))))
lst)

When I evaluate the resulting function in OM, I just get the input list as a result. What am I doing wrong here?

Dear David,

This is because of the rotatef macro. It works indeed, but the algorithm will sort all your list once.
So i tweaked it in order to have step by step sorting which i guess waht you really wanted. (Included in the zip, lisp file to put in patches + patch)

By the way this is really an amusing algoruthm. THanx for sharing
BEst
K

bubblesort.zip (1.38 KB)

Hi David, I think your code is also OK and should work in OM (see attached picture). Do you have a different result ?
J.

Capture-d’écran-2017-10-05-à-11.51.07.png

Hello Jean and Karim,

Karim’s tweaks solved my problem. Jean, the function would evaluate in OM, but (as Karim observed), it would only run once and just return the input list as an output. Thanks!

P.S.: Karim, glad you enjoyed the algorithm!

Sorry to ressurect such an old thread, but I’m having exactly the same issue as Matyas (I’m also trying to implement sorting in OM). I tried download Karim’s patch but OM is telling me the box is ‘dead’. Thanks!

Dear hbell25,

I suggest the following:

  1. open a new thread
  2. Please explicit the problem more clearly. ie, is it a coding problem, or a loading problem.

Thank you in advance.
K