5 ms·
The n-2 bit is unnecessary and misleading because we've already established that an index can be swapped with itself. for i in [0..n-1] swap(a[i], a[rand in
by andromeduck 4y ago
The n-2 bit is unnecessary and misleading because we've already established that an index can be swapped with itself.
for i in [0..n-1]
swap(a[i], a[rand in [i..n-1])
where rand in [I..n-1] coud be implemented as i + (rand() % (n-i))
essentially start with your finger at 0, pick a card from the unpicked set which is everything on and after your finger, swap that card with your finger, move your finger up one card