Created
May 20, 2016 14:07
-
-
Save onlylemi/ff1a616a29529b77a77bd70ed7b89206 to your computer and use it in GitHub Desktop.
【算法】随机排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 思想:随机交换 | |
* | |
* @param arr | |
*/ | |
public static void shuffle(int[] arr) { | |
Random r = new Random(); | |
int size = arr.length; | |
for (int i = size; i > 1; i--) { | |
int random = r.nextInt(i); | |
int temp = arr[i - 1]; | |
arr[i - 1] = arr[random]; | |
arr[random] = temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment