4 ms·
The Fisher-Yates algorithm has O(n) running time but it is slightly different than what the author suggested, instead of removing the element from the array, yo
by Znafon 6y ago
The Fisher-Yates algorithm has O(n) running time but it is slightly different than what the author suggested, instead of removing the element from the array, you swap it with the last, that way you are sure not to get it again by simply reducing the upper bound of your random function and it's a O(1) operation so the total running time is O(n): https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#T...