Given input integer array arr, shuffle the elements of the array and return array. Use Fisher-Yates algorithm to shuffle the array elements.
| Input Parameters |
Expected outputs |
| arr={1,2,3,4}
|
One possible output is given below (output will be different every time you run the program):2,4,3,1
|
| arr={-1,-2,-3,-4 }
|
One possible output is -3,-1,-4,-2
|
| arr={-3,-2,-1,0,1,2,3 }
|
One possible output is 1,-2,0,-3,3,-1,2
|
| The java method should accept following input parameters: arr (int array).
|
| Iterate through arr using a for loop using loop variable idx:
Use java Random class object to generate a shuffled index between 0 and arr.length-1.
Swap the values between arr[idx] and arr[shuffleIdx].
|
| When the above loop completes the array elements will be shuffled/randomized and we can return the array.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.Random;
public class ArrayShuffleRandomize {
public int[] arrayShuffleRandomize(int[] arr) throws Exception{
int temp=0;
Random rGen=new Random();
int shuffledIdx=0;
for (int idx=0; idx< arr.length; idx++) {
shuffledIdx=rGen.nextInt(0,arr.length);
temp=arr[idx];
arr[idx]=arr[shuffledIdx];
arr[shuffledIdx]=temp;
}
return arr;
}
public static void main(String[] args) {
ArrayShuffleRandomize ap=new ArrayShuffleRandomize();
int[] arr;
try {
int[] intArray1 = {1,2,3,4};
printArraySummary(intArray1, "Original Array");
arr=ap.arrayShuffleRandomize(intArray1);
printArraySummary(intArray1, "Shuffled Array");
int[] intArray2 = {-1,-2,-3,-4};
printArraySummary(intArray2, "Original Array");
arr=ap.arrayShuffleRandomize(intArray2);
printArraySummary(intArray2, "Shuffled Array");
int[] intArray3 = {-3,-2,-1,0,1,2,3};
printArraySummary(intArray3, "Original Array");
arr=ap.arrayShuffleRandomize(intArray3);
printArraySummary(intArray3, "Shuffled Array");
int[] intArray4 = {5,4};
printArraySummary(intArray4, "Original Array");
arr=ap.arrayShuffleRandomize(intArray4);
printArraySummary(intArray4, "Shuffled Array");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}