I wrote codes for the following question but they don't work. I get random numbers but the shuffle method don't shuffle them. Would you please help me?
for( each index i)
choose a random index j where j>=i.
swap the elements at index i and j.
My code is:
public static void shuffle(int[] a){
for( int i = 0; i < a.length-1; i++){
int range = a.length;
int j = (int) (Math.random() * range);
swap(a, i, j);
}
}
public static void swap(int[] array, int i, int j){
if (i != j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}