How do I optimize bubble sort loop?

Get help using Construct 2

Post » Tue Sep 30, 2014 10:42 pm

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 :P

Image
B
39
S
16
G
6
Posts: 542
Reputation: 7,617

Post » Tue Sep 30, 2014 11:53 pm

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.
ImageImageImage
B
71
S
21
G
231
Posts: 3,694
Reputation: 129,364

Post » Wed Oct 01, 2014 12:16 am

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 !
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Oct 01, 2014 12:33 am

Nice! Thanks a lot both of you :D
B
39
S
16
G
6
Posts: 542
Reputation: 7,617

Post » Wed Oct 01, 2014 12:34 am

... or you could just do a for each ordered by x ascending.
Image ImageImage
B
169
S
50
G
169
Posts: 8,288
Reputation: 108,220

Post » Wed Oct 01, 2014 12:43 am

I believe he was just trying his hand at bubblesorting for academic purpose, otherwise...
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Oct 01, 2014 1:56 am

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.
Image ImageImage
B
169
S
50
G
169
Posts: 8,288
Reputation: 108,220

Post » Wed Oct 01, 2014 11:35 am

Cool! Thanks for all the input. I'm learning lots of new things :) I actually *didn't* know there was built-in sorting in C2, but I didn't know I could pick by comparison or just use instance ID either. :oops: Anyways, here's v2:

Image

Oops, forgot to set Transmutation to 0 at the second line.
B
39
S
16
G
6
Posts: 542
Reputation: 7,617


Return to How do I....?

Who is online

Users browsing this forum: evillair, lordsas, Matei511, tarek2 and 60 guests