Make a bubble sort algorithm from scratch.

Oct 16, 2013 at 12:00pm

Make a bubble sort algorithm from scratch.

Ok so the challenge is this.
i need to create a bubble sort algorithm that can get a string of numbers

eg. 1 3 2 4 6 5

and convert it to

1 2 3 4 5 6

as the algorithm is designed for. i.e putting the numbers in ascending order.

now my problem goes like this. i dont want to use any fancy sorting objects. what i have in my head is a patch that starts with the string of jumbled numbers of any length in a message box. the numbers would then be funneled into a coll object with listfunnel putting them in the coll as follows:-

0, 1;
1, 3;
2, 2;
3, 4;
4, 6;
5, 5;

now at this point is where i run into problems……

what i need is a way to reference each ‘symbol/register’ and perform the sums on its contents.

now i dont know what i need here and iv scoured the web but here is my imaginary object that may or may not exist under a different name.

eg.

[GetVariableFromColl @Symbol 4]

using the [coll] from above, this “object” would return a value of 6 which is the number stored in the register/symbol referenced by the 4.

in this way i can process my numbers easily, using the registers which will not change meaning i can do it for any string not just this one.

now if anybody can help me with this it sure would help me out with all the headaches its giving me! im open to anything to help. i just really want to be able to do this at a very low level as apposed to just plugging my string into a [zl sort] box and it being done straight away i really want to get down and dirty with the algorithm.

that being said if anybody does know a way of organising a set of numbers into numerical order without using [zl sort] i would also be very interested in hearing about it.

i can add my patch so far if anybody wants to have a look but unfortunately the hurdle i am faced with is quite early in my chain so the patch doesent consist of much. :/

Many Many thanks in advance for any help you can offer i look forward to any responses!

MiRAGE

#268228
Oct 16, 2013 at 2:14pm

what i need is a way to reference each ‘symbol/register’ and perform the sums on its contents.

not sure if fully understand what the problem is,but to access the variable associated with an index you just send the index to coll

#268241
Oct 16, 2013 at 3:03pm

Hi maybe take a look at this thread I started a while ago. I ended up implementing bubble sort with jit.bsort. Its nice because you can specify how many iterations it does each time you give it an input. There are other sort methods in this thread too (hopefully they work as I havent looked at them in a bit).

Also it might help if you tell people the purpose of not using zl.sort etc.

All bests
Cormac

http://cycling74.com/forums/topic/conundrumed-and-listless-list-sorting-finding-closest-match-above-or-below/

#268246
Oct 17, 2013 at 12:16pm

You are describing the precise function of the coll itself… simply enter the index number and it will output the stored value.

Check out the coll help file… as well as tutorial 18.

– Pasted Max Patch, click to expand. –
#268313
Oct 17, 2013 at 12:40pm

Here is a bubble sorter I made specifically to output unique iterations of a sort. I opted to use a multislider to manage the indices using the ‘select’ message to change individual slider values. Each step in the sort compares two adjacent sliders and swaps values if they are not in order.

I’ve been meaning to tweak the patch to accept lists of any length (as well as accepting alphabetical elements) but for now, it’s set up for mod 8 lists (0-7). You can easily change the parameters of the multislider, counter, etc. as needed.

– Pasted Max Patch, click to expand. –
#268315
Oct 17, 2013 at 1:43pm

I should clarify that my patch doesn’t only output n-1 iterations (ala the maxiter attribute in jit.bsort), but all of the iterations within each iteration (inner loop variants).

ex. input 2 3 7 1 5 4 6 0

possible iterations with jit.bsort:

2 3 7 1 5 4 6 0
2 1 3 5 4 6 0 7
1 2 3 4 5 0 6 7
1 2 3 4 0 5 6 7
1 2 3 0 4 5 6 7
1 2 0 3 4 5 6 7
1 0 2 3 4 5 6 7
0 1 2 3 4 5 6 7

all possible iterations:

2 3 1 7 5 4 6 0
2 3 1 5 7 4 6 0
2 3 1 5 4 7 6 0
2 3 1 5 4 6 7 0
2 3 1 5 4 6 0 7
2 1 3 5 4 6 0 7
2 1 3 4 5 6 0 7
2 1 3 4 5 0 6 7
1 2 3 4 5 0 6 7
1 2 3 4 0 5 6 7
1 2 3 0 4 5 6 7
1 2 0 3 4 5 6 7
1 0 2 3 4 5 6 7
0 1 2 3 4 5 6 7

If it’s possible to get jit.bsort to output the latter, please share…

#268317

You must be logged in to reply to this topic.