Possible Duplicate:
How to randomize a javascript array?
I need to implement a randomized binary search tree (R-BST) out of an array so that the sorted array gives O(n lg n) average time and not the O(n^2) which is the worst case time if the arrays are already sorted or reverse sorted . Now the two steps are :
- Randomly permute the array A.
- Call BST sort (A) .
How do I go about doing the first step JavaScript? I want it so that each of the n!
permutations is equally likely to happen . I believe the way to do this in Java is Collections.shuffle
say something like :
Integer[] arr = new Integer[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
How would I do this in Javascript ? I can use jQuery.