HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Shuffling chunks of ints in an array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraychunksintsshuffling

Problem

The shuffle method in ShuffleMethods.java takes an array of ints and shuffles them by chunks of ints in another array. So for example, if we had an array of ints called nums comprising of 0,1,2,3,4,5 and a chunks of ints of 2,1,3. The method reads the first element of the chunks array which is 2. The shuffle method then gets the first two elements of the nums array 0 & 1 and puts them at the end of the array to give this result: 2,3,4,5,0,1, Then for the next chunks of 1 the number is 2 is selected and put on top of the 0,1 giving this result 3,4,5,2,0,1. And then finally the last chunks of 3 selects 3, 4, 5 and puts them on top of the 2 giving this result 3,4,5,2,0,1.

The shuffle method algorithm complexity is currently \$O(n^3)\$. I am looking to get it down to \$O(n)\$. Also, I am hoping to improve the pairs method which finds the number of consecutive numbers from the original deck that are still together in the shuffled deck.

ShuffleApp.java

package Shuffle;

import java.util.Arrays;
public class ShuffleApp{

public static void main(String[] args){
      ShuffleMethods test = new ShuffleMethods();
      int[] test1 = {2, 1, 3};

     test.makeNew(6);
     test.shuffle(test1);
     test.pairs();
     test.print();
   }

}


Shuffle.java

package Shuffle;
public interface Shuffle{
    public void makeNew(int size);
    public int[] getCurrent();
    public void shuffle(int[] chunks);
    public int pairs();
}


ShuffleMethods.java

```
package Shuffle;
import java.util.*;

public class ShuffleMethods implements Shuffle{

private int[] nums;
private int[] numsCopy;

/Fills nums with ints based on user parameter from 0 to size-1/
public void makeNew(int size){
nums = new int[size];
for(int i = 0; i count) {

temp = nums[0];

for(int k = 1; k pairList = new ArrayList();
int pairs = 0;

for(int i = 1; i < numsCopy.length; i++) {

if (numsCopy[i-1] == n

Solution

-
pairs is overcomplicated indeed. Notice that a block of size k contributes k-1 pairs, and no other pair exists. So the total is a sum of (k - 1) taken over all blocks. Final observation is that the sizes of blocks add up to the size of array, therefore pairs = num.size() - blocks.size() is all you need.

-
shuffle can be performed in linear time (strictly speaking, in \$O(kn)\$, where k is number of blocks) in place (\$O(1)\$ space complexity). The key algorithm is known as rotate. One possible implementation is

void rotate(int[] array, int first, int mid, int last) {
    reverse(array, first, mid);
    reverse(array, mid, last);
    reverse(array, first, last);
}


There also is an arguably faster implementation, where each element is moved exactly once. Try to figure it out yourself.

Code Snippets

void rotate(int[] array, int first, int mid, int last) {
    reverse(array, first, mid);
    reverse(array, mid, last);
    reverse(array, first, last);
}

Context

StackExchange Code Review Q#162084, answer score: 3

Revisions (0)

No revisions yet.