Solution: Shuffle Integers
This review discusses the solution of shuffling an array of 2n integers without using extra space.
Solution #1
It is safe to assume that for this problem the given array can be divided into two equal halves because the number of elements in the array is .
So, the naive (brute-force) approach to this problem is:
-
Use of two nested loops to transfer the elements in the right half of the array to the left half (lines 5-7).
-
Run the first loop half the size of array times, i-e,
size/2times, to traverse the elements in the second half of the array (line 5). -
Then, transfer the elements of the right half to the left half using the inner loop (lines 6-7).
Note that the
startindex in the second loop depends on which element we are rotating, and theendindex ...