Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / How to bubble sort!

Author
Message
ESC_
21
Years of Service
User Offline
Joined: 29th Aug 2003
Location: Mass.
Posted: 22nd Oct 2003 03:44 Edited at: 22nd Oct 2003 03:45
I just wrote a quick tutorial/code snippet on how to bubble sort a list of numbers in an array (this method can also be applied to sorting strings).

First off, why would you need to sort stuff?
There are numerous times that you might want to sort a list. Maybe you are making an RPG, and you wan't the player's inventory to be listed alphabeticly. Maybe you're doing a multiplayer game, and you want to list servers by # of players or ping.... ect. ect.


The bubble sort algorithm works like this:
start at the top of the array, and run through the array using a for loop. Compare each element of the array with the element below it. If the first element is greater than the second, swap 'em. It keeps going until the end of the array, then it runs through the algorithm again, until no swaps occur. The only big problem with bubble sorting is that it is a O(n2), which means the time it takes to calculate grows exponentially the more elements you try to sort.

Maybe if I feel brave over the weekend, I'll attempt a QuickSort example
(n log n by the way, which grows much slower than the exponential methods)

Code:

`bubble sort example
`by esc

sync on:sync rate 0:hide mouse:randomize timer()


`number of elements
length=25
`create the numerical array you wish to sort
dim numbers(length)


`randomize + print before sorting
set cursor 0,0
print "Before bubble sort:"
for i=0 to length
numbers(i)=rnd(100)
print str$(numbers(i))
next i

`wait for key, but update
while inkey$()=""
sync
endwhile
cls

t#=timer()
while sort=0
gosub bubblesort
endwhile
finaltime#=timer()-t#


print "After bubble sort:"
for i=0 to length
print str$(numbers(i))
next i

print "Time taken (in milliseconds)"
print finaltime#

do
sync
loop

bubblesort:
sort=1
for i=0 to length-1
if numbers(i)>numbers(i+1)
temp=numbers(i)
numbers(i)=numbers(i+1)
numbers(i+1)=temp
sort=0
endif
next i

return


"That's not a bug, it's a feature!"
"Variables won't, constants aren't."
ESC_
21
Years of Service
User Offline
Joined: 29th Aug 2003
Location: Mass.
Posted: 22nd Oct 2003 04:02 Edited at: 22nd Oct 2003 04:02
Bubble sorting a string array:
Code:

`bubble sort example
`by esc

sync on:sync rate 0:hide mouse:randomize timer()


`number of elements
length=10
`create the numerical array you wish to sort
dim strings(length) as string


`randomize + print before sorting
set cursor 0,0
print "Before bubble sort:"
strings(0)="Hello"
strings(1)="What's up?"
strings(2)="Hello"
strings(3)="Foo"
strings(4)="Baz"
strings(5)="Blah"
strings(6)="Testing"
strings(7)="Ahoy"
strings(8)="Ello"
strings(9)="Yo"
strings(10)="String"
for i=0 to length
print strings(i)
next i

`wait for key, but update
while inkey$()=""
sync
endwhile
cls

t#=timer()
while sort=0
gosub bubblesort
endwhile
finaltime#=timer()-t#


print "After bubble sort:"
for i=0 to length
print strings(i)
next i

print "Time taken (in milliseconds)"
print finaltime#

do
sync
loop

bubblesort:
sort=1
for i=0 to length-1
if strings(i)>strings(i+1)
temp$=strings(i)
strings(i)=strings(i+1)
strings(i+1)=temp$
sort=0
endif
next i

return


"That's not a bug, it's a feature!"
"Variables won't, constants aren't."
Codger
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 22nd Oct 2003 04:45
Look in code base for my quicksort example. You are welcome to change / modify it for your own purposes

System
PIII 650 MZ H.P. Pavillion
394 Mem GeForce 4 400MX
ESC_
21
Years of Service
User Offline
Joined: 29th Aug 2003
Location: Mass.
Posted: 22nd Oct 2003 04:52
Cool, I'll take a look

"That's not a bug, it's a feature!"
"Variables won't, constants aren't."
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Oct 2003 10:08
Quick-sort has got itself a name for being the quickest sort routine - this is only partially true.

If you add a single item to the end of your list, it is far faster to do a bubble-sort from the end of the list to get it into place than to do a quick-sort. A bubble-sort will get the item into the correct place in a single pass of the array.
ESC_
21
Years of Service
User Offline
Joined: 29th Aug 2003
Location: Mass.
Posted: 22nd Oct 2003 15:30
That is true, although in most situations you will get better results with a quicksort

"That's not a bug, it's a feature!"
"Variables won't, constants aren't."
Codger
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 22nd Oct 2003 17:43
If your list is in Order then Insert Item at the apropriate place in your array would be the fastest way I believe.

A recursive test to find the position then use the

"ARRAY INSERT AT ELEMENT Array Name(0), Index"

command

System
PIII 650 MZ H.P. Pavillion
394 Mem GeForce 4 400MX
ESC_
21
Years of Service
User Offline
Joined: 29th Aug 2003
Location: Mass.
Posted: 22nd Oct 2003 19:46
Yeah, I haven't really played with the dynamic array commands much, as I'd heard they weren't working up to the latest patch. I might try that later, though

"That's not a bug, it's a feature!"
"Variables won't, constants aren't."

Login to post a reply

Server time is: 2024-11-23 23:36:00
Your offset time is: 2024-11-23 23:36:00