FisherYates费雪耶兹随机置乱算法
1 public class FisherYates { 2 3 public static void main(String[] args) { 4 int[] arr = new int[10]; 5 // 初始有序数组 6 System.out.println("初始化有序数组:"); 7 for (int i = 0; i < arr.length; i++) { 8 arr[i] = i + 1; 9 System.out.print(" " + arr[i]); 10 } 11 // 费雪耶兹置乱算法 12 System.out.println("\n" + "费雪耶兹置乱算法+随机数:"); 13 14 for (int j = arr.length - 1; j > 0; j--) { 15 16 int random = (int) (Math.random() * 10); 17 System.out.print(" "+random); 18 int temp = arr[j]; 19 arr[j] = arr[random]; 20 arr[random] = temp; 21 } 22 System.out.println("\n"+"置乱后的数组:"); 23 for (int i : arr) { 24 System.out.print(" "+i); 25 } 26 27 } 28 29 }
gogogo