I came up with a very simple, naive algorithm to return shuffled array:
const shuffled = <Type,>(array: Type[]): Type[] => {
const items = array.map(
item => [ item, Math.random() ] as const
);
return items
.sort(
([ , a ], [ , b ]) => b - a
)
.map(
([ item, ]) => item
);
};
It just assigns a random number for each item and then sorts by these numbers. It seems to be 100% random, which is good. It also works without modifying the original array, which is what I want.
How much worse is the computational complexity comparing to more sophisticated shuffling algorithms?