The problem with a naive shuffle is that the value could already be replaced, and you can change it later. Say you have three cards, and you pick one random case for the first card. If later you can accidentally change this card last, then you select the randomness of this first choice.
If sorting is quicksort, it continuously splits the list by about half. The next iteration breaks each of these groups into two groups at random. This continues until you go to single cards, and then combine them all together. The difference is that you never take a card from a second randomly selected group and move it back to the first group.
A Knuth-Fisher-Yates shuffle is different from a naive shuffle because you only select a card once. If you were collecting random cards from the deck, would you put the card back and choose again? No, you take random cards one at a time. This is the first thing I heard about it, but I did something similar in high school, starting at index 0. KFY is probably faster because I have an extra complement in random expression.
for (int i = 0; i < cards.Length - 1; i++) { int n = rand.Next(cards.Length - i) + i; // (i to cards.Length - 1) Swap(ref cards[i], ref cards[n]); }
Do not think of it as an exchange, think of it by choosing random cards from the deck. For each element of the array (except the last one, because there is only one left), you select a random card from all the remaining cards and put it, forming a new stack of cards that are randomly shuffled. It does not matter that the remaining cards are no longer in the original order, if you have already done some swapping, you still choose one random card from all the remaining cards.
Random quick sorting is like taking a stack of cards and randomly splitting them into two groups, then selecting each group and randomly splitting them into two smaller groups, and so on, until you have separate cards and then back them up.
source share