This is a very hard but interesting probability question related to Knuth shuffle.
When looping for each element, the swap is performed for the current element with
This page compares the algorithm you mention with Knuth's, showing why Knuth's is better.
Note: the below might be wrong, see the comments.
I'm not aware of an easy way to compute the probability you ask about and I can't seem to find any simple explanation either, but the idea is that your algorithm (usually referred to as the naive shuffle algorithm) considers n^n
permutations of the array instead of n!
. This is because element i
can end up at each of n
positions at step i
. Since you have n
possibilities at each step and n
steps, that adds up to n^n
. Since n^n
is not always divisible by n!
it follows that not all permutations have the same probability, which is why the algorithm is considered a bad shuffle.
Since not all permutations have the same probability, it follows that the probability you ask about is different for different values of i
and j
, but I'm not aware of a formula that computes it.