Challenge 3 Shuffle the queue
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
public class QueueShuffle {
static void randomize( int arr[], int n)
{
// Creating a object for Random class
Random r = new Random();
// Start from the last element and swap one by one. We don't
// need to run for the first element that's why i > 0
for (int i = n-1; i > 0; i--) {
// Pick a random index from 0 to i
int j = r.nextInt(i);
// Swap arr[i] with the element at random index
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Prints the random array
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(10);
list.add(11);
list.add(12);
System.out.println("before shuffle: " + list);
Collections.shuffle(list, new Random());
System.out.println("Shuffled with Random: " +list);
}
}
QueueShuffle.main(null);