Efficient Blueprint shufling of items in array

Let’s say, I have an array of items:
A B C D E F ]
On an event, all elements of array are accessed one by one and at the end are shuffled
After 1-st event:
B C D E F A ]
After 2-nd event:
C D E F A B ]
and so on.

Naive idea would be to actually re-arrange items inside of the array:

  1. Copy items into a new array
  2. Place them back using “Set Array Elem” node with a new index
    Which means, making a full copy of array and one by one insert with each execution.

Second idea is to use an array of Ints, which are used as access indexes to original array.
So array of references always stay the same:
A B C D E F ]
but array of indexes goes as:
0 1 2 3 4 5 ]-> 1 2 3 4 5 0 ]-> 2 3 4 5 0 1 ]
Which means, I would need to set new values into index array every time.

Which one do you think is better in terms of performance, or perhaps there is a better, third way to do it?

Hello,
Another idea could be to have your array filled and a second one you fill when needed, select one random element, remove from array and loop to empty array.

Edit : example removed : You can use the node shuffle.

hey there,

do you want to shuffle or do you want to sort?

because your example describes a sorting algorithm and fen is presenting you a shuffle algorithm. (i am having troubles to look through blueprint code, so sorry if i got this wrong)

so if you want to have ABCDEF ->BCDEFA means sorting
if you want ABCDEF -> BDFECA is shuffled

However, when you ask about Performance maybe you should read into SortingAlgorithms and O-Notation, because there is no answer for you in your case.

You need to know that the efficiency depends on your amount of elements which needs to be sorted/shuffled.

Here is a little sort algorithm example:

n = Amount of Elements
i = 0

Step 1: Take First Element of Array and save it temporarily.
Step 2: Take Element at Place i + 1 (first round would be the second element), set this element at pos i (first elements position)
Step 3: increment i (i + 1)
Step 4: repeat step 2 until i + 1 == n, if i + 1 == n set your saved item at position i in array.

So you can see there would be no need of a second array, you only need one local item variable to save the first item (because it will be switched from far left to far right)

regards

Edit: its quite similar to bubble sort

Oh you absolutely right, . I saw shuffle and this is certainly sorting. You then simply add item in index 0 to array and then remove index 0.


array is string abcdef.

Edit : and about shuffle, i forgot the node “shuffle” which shuffle. Nothing to do more ^^

@anonymous_user_8d708650
In terms of performance I mean what is faster - add/remove array elements or change values stored in an array? I understand that answer can be different between C++ and Blueprints, it’s more of a general curiosity.

@Fen
This is awesome! Didn’t even though about just moving around the first element. Thank you!
Array Shuffle would be useful too, but I’ve confused the term. Sorry for that. I didn’t mean randomizing order, I meant just rolling values of the array in the circle, exactly like your blueprint does.