While studying Shuffle sort, I learnt Fisher Yates solution. It loops for 0 to array length and finds a random number between 0(inclusive) and the loop index (inclusive) and NOT 0 and N-1. Finding a random number between 0 and N-1 does not give a random solution. But I couldn't find the reason for the same.
public static void sort(Comparable[] a){
for(int i = 0 ; i < a.length ; i++){
int r = StdRandom.uniform(i+1);
// why cant this be a.length-1
exch(a, i, r);
}
}
Aucun commentaire:
Enregistrer un commentaire