A Flash Developer Resource Site

# Thread: Bubble Sort Help

1. ## Bubble Sort Help

I'm trying to code a bubble sort for the following array
I already have the scores from somewhere else...
they are the variables.

Scores= new array()
scores[0]= pOneScore
scores[1]=pTwoScore
scores[2]=pThreeScore

I jsut can't remember how to write exactly.
I want to find the highest score of those variables

2. ## bSort

Scores= new array()
scores[0]= 2;
scores[1]=6;
scores[2]=4;

temp = scores[0];
for(i=0;i<2;i++){
if(temp < scores[i+1]){
temp = scores[i+1]
}
}
trace(temp);

3. thanks..... so scores[0] becomes the highest score right?..how I do print it (trace it) in highest to lowest

4. sorry, i read it a bit wrong - this is closer

Scores= new array()
scores[0]= 12;
scores[1]=24;
scores[2]=6;

for(j=0;j<2;j++){
for(i=0;i<2;i++){
if(scores[i] < scores[i+1]){
temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;

}
}
}
for(i=0;i<3;i++){
trace(scores[i]);
}

5. That worked thanks... what I'm trying to do is build a game...and print out the order of the highest score first...now the problem is keeping the person's name with it.. cause I'm using all variables

pOneScore goes with playerOneOutput (first player score and name)
pTwoScore goes with playerTwoOutput (second player score and name)
pThreeScore goes with playerThreeOutput (third player score and name)

So I can make a multidementional array right?

6. sure can, or if you wish...

Scores= new array()
scores[0]= 12;
scores[1]=24;
scores[2]=6;

arrPeople= new array()
arrPeople[0]= "dick";
arrPeople[1]="tom";
arrPeople[2]="harry";

for(j=0;j<2;j++){
for(i=0;i<2;i++){
if(scores[i] < scores[i+1]){
temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;
tempPeople= arrPeople[i];
arrPeople[i] = arrPeople[i+1];
arrPeople[i+1] = tempPeople;

}
}
}
for(i=0;i<3;i++){
trace(scores[i] + " " +arrPeople[i]);
}

**********************8

if u r passing the arrays around alot(or have alot more attributes eg:sex, haircolor, age...) then use the md array. if not i would use a seperate array to keep it simple and move on to other things.

7. Yeah, I tried doing exactly what ya said...

scores= new array()
scores[0]= pOneScore;
scores[1]=pTwoScore;
scores[2]=pThreeScore;

names=new array()
names[0]=playerOneOutPut
names[1]=playerTwoOutPut
names[2]=playerThreeOutPut

for(j=0;j<2;j++){
for(i=0;i<2;i++){
if(scores[i] < scores[i+1]){
temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;
tempnames=names[i]
names[i]=names[i+1]
names[i+1]=temp;

}
}
}
for(i=0;i<3;i++){
trace(names[i]);
trace(scores[i]);
}

But for some reason.. it doesn't always print it out right. It'll show the top score...but not the rest...

8. tempnames=names[i]
names[i]=names[i+1]
names[i+1]=temp; <<that var needs to be "tempnames" instead of "temp"

9. thanks, check this out

scores= new array()
scores[0]= _root.pOneScore;
scores[1]=_root.pTwoScore;
scores[2]=_root.pThreeScore;

names=new array()
names[0]=_root.playerOneOutPut
names[1]=_root.playerTwoOutPut
names[2]=_root.playerThreeOutPut

for(j=0;j<2;j++){
for(i=0;i<2;i++){
if(scores[i] < scores[i+1]){
temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;
tempnames=names[i]
names[i]=names[i+1]
names[i+1]=tempnames;

}
}
}

_root.firstPlaceName = names[0];
_root.firstPlaceScore = scores[0];

_root.secondPlaceName = names[1];
_root.secondPlaceScore = scores[1];

_root.thirdPlaceName = names[2];
_root.thirdPlaceScore = scores[2];

Thats what it looks like right now....but the problem is, if the 3rd person ties the high score, it doesn't sort.. Any suggestions??

10. ## if the 3rd person ties

_root.createTextField("tField1",0,50,50,300,100)
_root.tField1.text = "YOU MUST BEAT THE HIGH SCORE TO RANK!";

11. what does that do? I don't understand?

12. ## more conditions

if you want a tie to beat the high score
you'll have to put in a condition
that checks for equal scores.

if (the scores are equal) {
then do this;
and this;
and this;
}

if you want it to sort the scores that are equal,
you'll need some more decisions...

:: or you could (not recomended) ::

NOT use integer scores,
go to a far decimal point(or high score)
(less chance of a tie).
high score = 10.28591 or 13,423,677

^ that is sloppy, but if you want to play the game, play.
if you want to code,
code some more decisions in and
show me what you have when you are getting warmer

13. I attached the file.... Check it out I'm either not explaining what's happening right or something. It just doesnt seem to be doing the Sort right... If the Third one is the Highest Score, then it sets the other 2 answers to the score of the Second Place Score.

14. mmmmm, sorry bout that chap, give me a bit...

15. ## condition to '<=' instead of '>'

ok,

change the condition to '<=' instead of '>' (or whatever it was)
let me know if i am mistaken, or confused!

for(j=0;j<2;j++){
for(i=0;i<2;i++){
if(scores[i] <= scores[i+1]){
temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;
tempnames=names[i]
names[i]=names[i+1]
names[i+1]=tempnames;

}
}
}

16. haha..that's exactly what I've been doing... I've been working on this awhile.. I figured if you had the file you could see exactly whats happening.. I'm new to ACtionSCript, but what I do know, i understand it, plus I'm good at other programming..but right now, I'm stumped cause it works sometimes, but not always

17. hey man.. I got it to work. I had one of the output text boxes named wrong. I thought I checked it a million times but I guess I kept overlooking it.. Thanks for all the help

18. cheers

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width