How do I optimize bubble sort loop?

0 favourites
  • 8 posts
From the Asset Store
2D Cartoon bubble shapes icons and bubble pop animation
  • Just wanted to get some second opinions/tips for a bubble sort function I'm writing. I have ten Character object instances that I want to sort according to where they are along the X axis. Not sure I understand completely how C2 events work when it comes to object instances, but if I got it right I can't reference multiple instances of the same object directly within the same function call. So what I'm doing is run a couple of extra For loops to get to the Character instances I want to reference later, then plunk the values I need from them into local variables that I use for comparison further down the line.

    But that makes for an awful lot of loops within a function that's set to run every tick. I'm worried. Will all this nested looping brutalize weaker computers? Am I doing it wrong/being stupid? Is there a better way? I'd really appreciate any tips or get some reassurance that it's alright

  • You don't need the two Character loops - just Pick by comparison/evaluate. Aside from that, if you place Character in a Family, you can then avoid the single reference issue as you can iterate over that Family against the Character - just be sure to skip the same object.

  • And be sure to add a check at the end of each pass to see if at least one permutation was done. If no permutation was made, it means the sequence is already in order, so "stop loop" ! Otherwise it'll be "worst case scenario" every time !

  • Nice! Thanks a lot both of you

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • ... or you could just do a for each ordered by x ascending.

  • I believe he was just trying his hand at bubblesorting for academic purpose, otherwise...

  • It would be interesting to see all the sorting types made up in events.

    I think the object index would be a much better than the id however. Especially since you can reference it in actions sprite(index), sprite(index+1), etc.

  • Cool! Thanks for all the input. I'm learning lots of new things Anyways, here's v2:

    Oops, forgot to set Transmutation to 0 at the second line.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)