I have a quick sort function built in blueprints based off of a C++ example to sort an array. I have built, at least I think I have, it to match the C++ example exactly. I have a blueprint version and a C++ version of SortArray and it seems that the C++ version works while the blueprint version doesn’t. I’m wondering if blueprints just aren’t fast enough? Here is the sort array in blueprints. It starts with this function (public) taking an array (value) setting it to a local variable and calling Quicksort. Quicksort takes the local variable pass by reference and sorts it.
In Quicksort it calls another function, Partition, which again passes by ref It is also a recursive function which I’ve read elsewhere work just fine in blueprints.
The array I am sorting is:
{5,0}
{2,1}
{8,2}
I am only sorting the X value of the Vector2D array. Below is the C++ version of the same function:
TArray<FVector2D> UMyBlueprintFunctionLibrary::SortArray(const TArray<FVector2D>& MyArray, int32 top, int32 bottom)
{
TArray <FVector2D> tempArray = MyArray;
QuickSort(tempArray, bottom, top);
return tempArray;
}
void UMyBlueprintFunctionLibrary::QuickSort(TArray<FVector2D>& myArray, int32 top, int32 bottom)
{
int middle;
if (top < bottom)
{
middle = Partition(myArray, top, bottom);
QuickSort(myArray, top, middle); // sort first section
QuickSort(myArray, middle + 1, bottom); // sort second section
}
return;
}
int UMyBlueprintFunctionLibrary::Partition(TArray<FVector2D>& myArray, int32 top, int32 bottom)
{
FVector2D x = myArray[top];
int i = top - 1;
int j = bottom + 1;
FVector2D temp;
do
{
do
{
j--;
} while (x.X < myArray[j].X);
do
{
i++;
} while (x.X > myArray[i].X);
if (i < j)
{
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
} while (i < j);
return j;
}
I pass in the same data for both; The Vector2D array, its LastIndex for Top, and 0 for Bottom. The C++ correctly sorts the array while the Blueprint version doesn’t seem to do anything. The reason I’m posting this even though I have a working sorting function is because I’d prefer to keep everything in Blueprints.
If anyone has feedback or a different sorting method I would greatly appreciate it.