Hey everyone,
I made a demo of the "Cocktail Sort". I have attached the project, as well as included the source code here.
Figure it might help individuals who need to figure out how to do sorting on User-Defined Types in AppGameKit (also Dark Basic too!)
To run, copy-paste this entire code into the "main.agc" file of a new blank project. This code, as is, sorts the X and Y fields of the generic type - the X is the primary sorting field whereas the Y is secondary.
rem Portrait App
SetDisplayAspect( 0.66 )
rem defile our generic type
type Generic_Data_Type
X as integer
Y as integer
endType
rem store how many values will be in the list
Global GenericListCount as integer
GenericListCount = 25
rem make an array of it
Global Dim GenericList[GenericListCount] as Generic_Data_Type
rem populate it with random numbers
rem remember arrays are 0-based
i as integer
for i = 0 to GenericListCount - 1
GenericList[i].X = Random()
GenericList[i].Y = Random()
next i
rem now sort the list
SortGenericList()
rem render
do
rem print off contents of the sorted list to the screen
for i = 0 to GenericListCount - 1
print(str(GenericList[i].X) + "," + Str(GenericList[i].Y))
next
Sync()
loop
rem this is the function that actually sorts through the items in the list
function SortGenericList()
rem list is sorted if we make no swaps
rem if we swap values, we set this to 1
swapped as integer
rem counter variable
i as integer
rem now the magic works
repeat
rem at the beginning of each iteration, set swapped to 0
swapped = 0
rem always start at 1, and not 0. we need two items to compare against
rem start from 1 upwards
for i = 1 to GenericListCount - 1
rem < causes items to be sorted in descending order
rem > causes items to be sorted in ascending order
if GenericList[i - 1].X < GenericList[i].X
SwapGenericListItems(i)
swapped = 1
elseif GenericList[i - 1].X = GenericList[i].X
if GenericList[i - 1].Y < GenericList[i].Y
SwapGenericListItems(i)
swapped = 1
endif
endif
next
rem if no swapped has taken place now, there won't be one when we go backwards
rem exit loop.
rem to do a normal bubble sort, delete everything from this line
rem ............................................................
if swapped = 0
exit
endif
rem now to do a cocktail sort, instead of a regular bubble sort, we do the exact same sort as above
rem but start at the top of the list and work downards to 1
for i = (GenericListCount - 1) to 1 step -1
rem < causes items to be sorted in descending order
rem > causes items to be sorted in ascending order
if GenericList[i - 1].X < GenericList[i].X
SwapGenericListItems(i)
swapped = 1
elseif GenericList[i - 1].X = GenericList[i].X
if GenericList[i - 1].Y < GenericList[i].Y
SwapGenericListItems(i)
swapped = 1
endif
endif
next
rem .............................................................
rem to do a normal bubble sort, delete everything above this line
until swapped = 0
endfunction
rem this function swaps items within the array
function SwapGenericListItems(pIndex as integer)
tX as integer
tY as integer
rem assign temp variables values so we can accomodate space for the swap
tX = GenericList[pIndex - 1].X
tY = GenericList[pIndex - 1].Y
rem swap one set of values into their new place
GenericList[pIndex - 1].X = GenericList[pIndex].X
GenericList[pIndex - 1].Y = GenericList[pIndex].Y
rem swap the other set of values into their place
GenericList[pIndex].X = tX
GenericList[pIndex].Y = tY
endfunction
Hi there. My name is Dug. I have just met you, and I love you.