Solution: Shuffle Integers
This review discusses the solution of shuffling an array of 2n integers without using extra space.
Solution #1
Press + to interact
main.java
Helper.java
class Shuffle{public static void shuffleArr(int arr[], int size) // Swap elements from left to right{for (int i = 0, q = 1, k = size; i < size; i++, k++, q++)for (int j = k; j > i + q; j--)Helper.swap(arr, j-1, j);}public static void main( String args[] ){int [] arr = {3, 5, 7, 9, 11, 13};shuffleArr(arr, arr.length/2);System.out.println( Arrays.toString(arr) );}}
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/2
times, to traverse the elements in the second half of the array (line 5 ...
Access this course and 1400+ top-rated courses and projects.