CycleShift

 

Download

 

Godot 4.x.

Python (Not done yet, will implement when the Godot version is done).


CycleShift was a total accident...


CycleShift is an algorithm that accidentally sorts a randomly ordered list of non-repeating numbers. During this process the numbers jump around from position to position - some in a pattern, some randomly - allowing them to be sampled in a deterministic way. Eventually the numbers will sort - this is called the paradigm - and the process is complete, or you can just keep iterating over the numbers to keep things going, however it will now be the same set of random numbers much like regular seeding of random number generators.


How it works

 

Say you have a random array who's values are;


[2, 0, 1, 3]



This unsorted state will become the `first` and `current` Array of the CycleShift process. The first step is to create an Array to compare the `next` result to check if the numbers are sorted. Therefore making the paradigm Array equal to:


[0, 1, 2, 3]

 


The CycleShift process works like this;

for each NUMBER in FIRST:
    add the value of index NUMBER of the CURRENT array to NEXT.
set CURRENT to NEXT.

If this process is done enough times, the numbers will sort from least to greatest.

First:          [2, 0, 1, 3]
Iteration 1: [1, 2, 0, 3]
Iteration 2: [0, 1, 2, 3]    <- Sorted
Iteration 3: [2, 0, 1, 3]    <- Loops back to the start

Here's a visual representation of how it works:


Afterthoughts


It is still a bit beyond me how this action sorts numbers from least to greatest, but putting the algorithm into ChatGPT said it looks like sorting by permutation cycle and index based sorting. I asked GPT what it'd name the algorithm, it was heavily biased towards CycleShift, so that became its name.