QB64 Phoenix Edition
Best Bubble Sort - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: QBJS, BAM, and Other BASICs (https://qb64phoenix.com/forum/forumdisplay.php?fid=50)
+--- Thread: Best Bubble Sort (/showthread.php?tid=2113)



Best Bubble Sort - dbox - 10-20-2023

I just saw where Spriggsy posted this great new algorithm.

Click this link to try it out in QBJS.


RE: Best Bubble Sort - bplus - 10-20-2023

(10-20-2023, 07:36 PM)dbox Wrote: I just saw where Spriggsy posted this great new algorithm.

Click this link to try it out in QBJS.

This is cool but where does the title of this thread come from? I was all set to b... about Bubble Sort! ;-))


RE: Best Bubble Sort - mnrvovrfc - 10-20-2023

"However, better sort algorithms exist."
-- Herbert Schildt, The Art of C

Something was said by him at least three times, while he also went over selection sort and something else that wasn't quick sort. LOL at "best" bubble sort, it's like choosing a beer for the "best" bubbles and never drinking it. (raises hand)


RE: Best Bubble Sort - bplus - 10-20-2023

Best bubbles got it!




RE: Best Bubble Sort - NakedApe - 10-24-2023

(10-20-2023, 11:24 PM)bplus Wrote: Best bubbles got it!


That's cool and so few lines of code. I don't get your loop starting WHILE 1 ... WEND though. How does that work? While what's 1? Scratches head . . .


RE: Best Bubble Sort - bplus - 10-24-2023

(10-24-2023, 06:00 PM)NakedApe Wrote: ...

That's cool and so few lines of code. I don't get your loop starting WHILE 1 ... WEND though. How does that work? While what's 1? Scratches head . . .

While looks for Boolean expression that evaluates to 0 or not 0 eg 1

So While 1 sets up an infinite loop that needs an escape clause inside it or depends on top right X box to close program. Better would have been:
While _KeyDown(27) = 0

bubble was from some really old code, 6 years I think, ya live, ya learn if you do well.


RE: Best Bubble Sort - mnrvovrfc - 10-24-2023

(10-24-2023, 06:00 PM)NakedApe Wrote: That's cool and so few lines of code. I don't get your loop starting WHILE 1 ... WEND though. How does that work? While what's 1? Scratches head . . .

It actually creates an endless loop. It is assumed that inside the loop, there is a condition to break out of it. However, `WHILE... WEND` is not recommended to be used in this programming system simply because there is no `EXIT WHILE`. Freebasic could do that, but QB64 cannot.

Therefore, the recommended thing is:
Code: (Select All)
DO WHILE 1
'some code
IF condition THEN EXIT DO
'some more code
LOOP

With `DO... LOOP` it is also possible to do `LOOP WHILE 0` or `LOOP UNTIL 1`, then in that case you have a "loop" that executes once. This is ideal when you have a deeply-nested loop and well within, you need to break out of all the levels with `EXIT DO`. I learned about this in the QB64 Wiki.

The "condition", as bplus already said, should involve getting a keypress from the user, especially ESCAPE key, to get out of the program.


RE: Best Bubble Sort - bplus - 10-24-2023

>"It actually creates an endless loop. It is assumed that inside the loop, there is a condition to break out of it. However, `WHILE... WEND` is not recommended to be used in this programming system simply because there is no `EXIT WHILE`. Freebasic could do that, but QB64 cannot."<

QB64 can EXIT WHILE don't mislead please
Code: (Select All)
While 1
    i = i + 1
    If i >= 10 Then Exit While
    Print i
Wend



RE: Best Bubble Sort - NakedApe - 10-31-2023

Thanks for the replies!