Quick sort not working in Blueprints

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. :smiley:

Upon closer inspection I’ve found in the C++ example that the initial passing of top and bottom are switched. When debugging the blueprint I found that Partition was never being called because of this, however now I am getting Infinite loops up the wazoo.

Ok I have figured it out. I ended up using a different method (one I adapted from a C# example [here][1]). I couldn’t figure out why the other one wasn’t working though I know where it was getting stuck. I did end up making a special while loop with break macro because of the way they’re handled in blueprints. They look a little messier but that’s because of the pass by ref needing a direct link.

You could probably replace those ugly Branch sections with while loops but it made it easier to Step-by-Step debugging.

This doesnt seem to work all the time. If it feed it a varied array like the one mentioned in the first post it works. However if the array has all 0’s for x values in each index it inf loops out.