Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To shuffle an array of integers into random order, a pseudo-code is developed as

ID: 3880966 • Letter: T

Question

To shuffle an array of integers into random order, a pseudo-code is developed as follows:

elementsRemaining = size of array
while elementsRemaining > 1
o Randomly choose index rndNdx in range 0 – elementsRemaining-1
o Swap element at rndNdx with element at elementsRemaining-1
o Decrement elementsRemaining

Based on the pseudo-code, implement the shuffle() function below by filling the missing statements:

function shuffle(arr) {

// Some statements are missing here


}

Write ONLY the missing statements – do NOT rewrite the entire code given above. You may need
to create some local variables in side the function body in order to make the shuffling work
correctly. Mind the programming style (naming convention, indentation, spacing, etc.) in your code.

Explanation / Answer

function shuffle(arr) {
Random random=new Random();//used for generating random number in jaav
int elementsRemaining=n;//n is size of array
int rndNdx=0;//this variable will be used to hold random number generator
while(elementsRemaining>1)
{
int rndNdx=random.nextInt(elementsRemaining - 0 + 1) + 0;//generating random number using rand with in the range of 0 and elementsremaining-1
//below tree are steps for swapping the code
int temp=arr[rndNdx];
arr[rndNdx]=arr[elementsRemaining-1];
arr[elementsRemaining]=temp;
elementsRemaining==;//decrementing the variable
}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote