Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best Bubble Sort
#1
I just saw where Spriggsy posted this great new algorithm.

Click this link to try it out in QBJS.


Attached Files
.zip   rr.zip (Size: 93.86 KB / Downloads: 128)
Reply
#2
(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! ;-))
b = b + ...
Reply
#3
"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)
Reply
#4
Best bubbles got it!

b = b + ...
Reply
#5
(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 . . .
Reply
#6
(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.
b = b + ...
Reply
#7
(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.
Reply
#8
>"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
b = b + ...
Reply
#9
Thanks for the replies!
Reply




Users browsing this thread: 1 Guest(s)