[bb] Sorted Linked Type Lists by Miracle [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Sorted Linked Type Lists
Author : Miracle
Posted : 1+ years ago

Description : This sort algorithm is essentially a bubble sort, which can be slow especially for very long lists.  

Code :
Code (blitzbasic) Select
Global NUM_COWS = 8

Type cow
Field z
Field pr.cow
Field nx.cow
End Type

Type list
Field firstcow.cow
Field lastcow.cow
End Type

Global list.list = New list
For t = 1 To NUM_COWS
moo.cow = New cow
mooz = Rand(0,100)
SortCowsOnZ(moo)
Next

Print "Unsorted list:"
For moo.cow = Each cow
Print mooz
Next
Print
Print "Sorted list:"
moo = listfirstcow
Repeat
Print mooz
moo = moo
x
Until moo = Null

WaitKey()

End

Function SortCowsOnZ( cow.cow )
moo.cow = listlastcow
done = 0
If cow
x <> Null And cowpr <> Null Then
If cowpr <> Null Then cowpr
x = cow
x
If cow
x <> Null Then cow
xpr = cowpr
EndIf
If moo <> Null Then
Repeat
If mooz >= cowz Then
cowpr = moo
cow
x = moo
x
If moo
x <> Null Then moo
xpr = cow
moo
x = cow
done = 1
EndIf
moo = moopr
Until moo = Null Or done = 1
EndIf
If done = 0 Then
cow
x = listfirstcow
If listfirstcow <> Null Then listfirstcowpr = cow
EndIf
If cow
x = Null Then listlastcow = cow
If cowpr = Null Then listfirstcow = cow
End Function


Comments : none...