PDA

View Full Version : Flash MX bubblesort algorithm code. plz help



resonanceshah
May 30th, 2005, 08:19 PM
Hi guys,

I have had one frustrating day with Flash today. Have been on it for 8 hours trying to figure out whats wrong with my code. Am counting the number of horizontal walls and storing their respective coordinates in space (Y coordinates only as it is constant for a horizontal wall).

Having done that am sorting the walls in order of their positions in space.

Example:

hor_num ==> no. of walls (3)
horizontal_wall[1]==>stores the y position of wall 1 in space and so on.

-------------------------------------

trace("Actual values found: "+horizontal_wall);



for (j=0;j<hor_num;j++)
{
p=0;

while(p<=hor_num)
{

if (horizontal_wall[p] > horizontal_wall[p+1])
{

// Swap

temp1=horizontal_wall[p];
horizontal_wall[p]=horizontal_wall[p+1];
horizontal_wall[p+1]=temp1;

}
p=p+1;

}

}

trace("After sorting values found: "+ horizontal_wall);

--------------------------------------

The output is really weird.

Actual values found: 200,300,50
After sorting values found: 200,,300,50

For some reason it gives 2 comma characters and another thing which i found after hours of debugging was that the if condition becomes true even though the first value is smaller than the second one.

Hope somebody out there can resolve this problem. Would really appreciate

Blackspirit
May 31st, 2005, 05:29 AM
I noticed your swapping items in an array, what I can also see is that when p gets to 2, horizontal_wall[p+1] becomes horizontal_wall[3], which is bad, as they'res no fourth element in your array.

while(p<=hor_num)

maybe change this to

while(p<hor_num-1)

then it wont try to swap the last element with a non-existing one.

resonanceshah
May 31st, 2005, 09:29 AM
[QUOTE=Blackspirit] Thanx a lot for your reply. Well changin the while to just < did help resolve the problem of the extra comma. But for some reason my program still does not go into my if statement even though the condition becomes true. Now why would that happen. I put an extra trace command inside my if statement, but it rever got run.Ideally it should have entered the If statement twice.

Blackspirit
May 31st, 2005, 11:45 PM
Ok, before you go into the if statement have a

trace(horizontal_wall[p] +"," +horizontal_wall[p+1]);

and then

trace("is it true?:" + (horizontal_wall[p] > horizontal_wall[p+1]));

I getting the feeling the numbers may actually be considered strings, so it may be the case of converting in the if statement...

if(number(horizontal_wall[p]) > number(horizontal_wall[p+1]))

resonanceshah
June 1st, 2005, 09:55 AM
thanxxxxxx a lottt. that really solved the whole problem. Never realized that flash might be taking it as string. Thanx again.