Quote: "Thank you for the contribution! However, I think there is a bug with your code. "
Quote: "The UDT's values are supposed to be kept grouped together. Instead, this insertion sort sorts out the fields independently of each other."
Actually, that's not a bug. It's me misinterpreting your intentions.
Ok answerman, this had better work because it took me ages to write and debug this (a single typo was causing the whole thing to crash!!!).
// our UDT
type TYPE_WATER_DROPLET
X as integer //---- third sorted field, descending
Y as integer //---- second sorted field, descending
active as integer //---- first sorted field, descending
id as integer //---- not sorted on
endtype
// NEW TYPE
type INSERTION_SORT_ARRAY
Value as integer
A_Type as integer
endtype
// Create an array
SizeOfArray = 10
Dim Array[SizeOfArray] as Type_Water_Droplet
// Fill each element of the array with a random number
For n = 0 to SizeOfArray
Array[n].X = Random(1, 1000)
Array[n].Y = Random(1, 1000)
Array[n].active = Random(1, 1000)
Next n
// Start the clock
BeginTime# = timer()
// Create a separate array and fill it
NewArraySize as integer
NewArraySize = 33
Dim SortArray[NewArraySize] as INSERTION_SORT_ARRAY
// Fill this array with values of the original array we want sorted
For n = 0 to SizeOfArray // size of original array
// X's // Using our A_Type values above
SortArray[n].Value = Array[n].X
SortArray[n].A_Type = 1
// Y's // Using our A_Type values above
SortArray[n+11].Value = Array[n].Y
SortArray[n+11].A_Type = 2
// Actives // Using our A_Type values above
SortArray[n+22].Value = Array[n].Active
SortArray[n+22].A_Type = 3
Next n
// Sort each element
// Using the insertion sort
CurrentValue as integer
CurrentIndex as integer = 1
SortIndex as integer
BeginTime# = timer()
While CurrentIndex <= SizeOfArray
SortIndex = 0
// dealing with the already sorted half of the array
While SortIndex < CurrentIndex
// sort the x's
If SortArray[CurrentIndex].Value > Array[SortIndex].Value
// Store the larger value and then shuffle the sorted part down
TempValue = SortArray[CurrentIndex].Value
TempType = SortArray[CurrentIndex].A_Type
// shuffle the portion of the sorted part of the array down to make room for the value to be inserted
SortCount = SortIndex
EndIndex = CurrentIndex
While EndIndex > SortCount
SortArray[EndIndex].X = SortArray[EndIndex-1].X
SortArray[EndIndex].A_Type = SortArray[EndIndex-1].A_Type
EndIndex = EndIndex - 1
Endwhile
// insert the larger value into its correct place
SortArray[SortIndex].Value = TempValue
SortArray[SortIndex].A_Type = TempType
Endif
// increase the sort index
SortIndex = SortIndex + 1
Endwhile
// Increase the current index
CurrentIndex = CurrentIndex + 1
Endwhile
// Copy the sort algorithm back into the original algorithm
// Counts for each element we are sorting
Xcount as integer = 0
Ycount as integer = 0
ActiveCount as integer = 0
For n = 0 to NewArraySize
If SortArray[n].A_Type = 1
Array[Xcount].X = SortArray[n].Value
Xcount = Xcount + 1
Else
If SortArray[n].A_Type = 2
Array[Ycount].Y = SortArray[n].Value
Ycount = Ycount + 1
Else
If SortArray[n].A_Type = 3
Array[ActiveCount].Active = SortArray[n].Value
ActiveCount = ActiveCount + 1
Endif
Endif
Endif
Next n
Endtime# = timer()
ElapsedTime# = EndTime# - BeginTime#
// create a bunch of text objects to display our results
For n = 0 to SizeOfArray
//For the x's
CreateText(n, str(Array[n].X))
SetTextPosition(n, 0, n*8)
// For the y's
CreateText(n+11, str(Array[n].Y))
SetTextPosition(n+11, 20, n*8)
// The actives
CreateText(n+22, str(Array[n].Active))
SetTextPosition(n+22, 40, n*8)
Next n
// Create a text object for the elapsed time
CreateText(40, str(ElapsedTime#) + " sec")
SetTextPosition(40, 60, 80)
// a loop
do
sync()
loop
It's poorly commented because I was pretty frustrated when I decided to retype the whole thing after copy/pasting the bugged code into DBP and solving it from there.
It's hard to see that it has actually sorted the array from the results printed but run it through your tests and let me know the results.
Quote: "The items with an active value of 0 are not sorted because they're ignored by the game during gameplay anyway, and I'd rather not spend time sorting values that won't be used anyway."
I'm sure you can fix that up.
Edit: Forgot to explain what it actually does. What it does is creates a new UDT array, one field for storing the value and the other for storing the element which it came from. For example you have this:
Array[1].X = 10
Array[1].Y = 40
Array[1].Acive = 0
In the new array the above would look like this:
The X
SortArray[1].Value = 10
SortArray[1].A_Type = 1
The Y
SortArray[2].Value = 40
SortArray[2].A_Type = 2
The Active
SortArray[3].Value = 0
SortArray[3].A_Type = 3
Using the A_Type legend:
X = 1
Y = 2
Active = 3.
So all of those values are stored into the new array along with their 'types', are then sorted, and then copied back into the orginal array into their correct fields using the A_Type value.
I hope that makes sense.