Jumble Array Values

An array A is of size N. Write a java method that jumbles all values in the array. All possible permutations must be possible and chances for each possible permutation must be equal. For example, size of array A is 3. A[0]=x, A[1]=y, A[2] =z.
Possible permutations are 3! After jumbling elements, there would be equal chances for each of xyz,yxz,... to be in the array elements.

Questions by venkatalaks

Showing Answers 1 - 27 of 27 Answers

vinshv

  • Sep 27th, 2008
 

public class Main {
    static String[] A = new String[] {"1", "2", "3", "4"};
  
    private static String toString(String[] s) {
        String result = "";
        for (int k=0; k < s.length; k++) {
            result += s[k];
        }
        return result;
    }

    private static void swap(int i, int j) {
        String tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
    }

    public static void main(String[] args) {
        int i, j;
 
        System.out.println(toString(A));

        for (i = 0; i < A.length; i++ ) {
            for (j = i+1; j < A.length; j++) {
                swap(i, j);
                System.out.println(toString(A));
            }
        }
    }
}

  Was this answer useful?  Yes

rjlast888

  • Oct 17th, 2008
 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class TestProgram
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {

        int n = 20;
        List a = new ArrayList(n);
        List randArray = new ArrayList(n);
        Map usedMap = new HashMap();
        
        for(int idx=0; idx < n; idx++)
        {
            a.add(new String(Integer.toString(idx)));
        }
        
        Random generator = new Random();
        for(int idx=0; idx < a.size(); idx++)
        {
            int position = getPosition(generator, usedMap, a.size());
            randArray.add(a.get(position));
        }
        
        for(int idx=0; idx < randArray.size(); idx++)
        {
            System.out.println((String)randArray.get(idx));
        }
        

    }

    private static int getPosition(Random generator, Map usedMap, int arraySize)
    {
        int pos = -1;
        
        while(pos == -1 && !usedMap.containsKey(pos))
        {
            float f = generator.nextFloat() * arraySize;
            int irand= (int)f;
            if(irand == arraySize)
            {
                irand--;
            }
            pos = irand;
        }
        
        usedMap.put(pos, null);
        
        return pos;
    }

}

  Was this answer useful?  Yes

c4crab

  • Oct 30th, 2008
 

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        HashMap hm = new HashMap();
        int arr[] = new int[20];
        for (int i = 0 ;i<20; i++)
        {
            arr[i]=i;
            hm.put(String.valueOf(new Random().nextInt()),String.valueOf(i));
        }
        Collection arr1 = hm.values();
        Iterator i = arr1.iterator();
        while (i.hasNext())
        {
            System.out.println((String)i.next()+" ");
        }
    }
}

jweir

  • Nov 25th, 2008
 

void jumble(Object[] A) {
  Random rand = new Random(System.currentTimeMillis());
  Object temp;
  for (int i = 0; i < A.length; i++) {
    int idx = rand.nextInt(A.length);
    temp = A[idx];
    A[idx] = A[i];
    A[i] = temp;
  }
}

jwfakejr

  • Jun 2nd, 2009
 

package javatest;

import java.util.ArrayList;
import java.util.Collections;

public class Main {
   
public static void main(String[] args) {

ArrayList<Integer> mylist = new ArrayList<Integer>();
for(int j = 0; j<20; j++)
{
  mylist.add(j);
 

}
Collections.shuffle(mylist);
System.out.println("n--------------");
for(int j = 0; j<20; j++)
{
 System.out.print(mylist.get(j)+" ");

}
// print it out again just to show it shuffles up the arrays contents
System.out.println("n--------------");
Collections.shuffle(mylist);
for(int j = 0; j<20; j++)
{
 System.out.print(mylist.get(j)+" ");

}
}
}

vijusatya

  • Jun 5th, 2009
 

public void jumble()
{
String a=new String{"A","B","C"};
ArrayList ar=new ArrayList();
for (i=0;i<=arr.length;i++)

{

String str=new String(a[i]);
for(j=0;j<=arr.length;j++)
{
if(!(a[i].equals(a[j])))
str=str+a[j];

}
if(!ar.contains(str)
{
System.out.println(str);
ar.add(str);
}

}

Sarje

  • Aug 13th, 2009
 

class JumbleArray
{
         public static void main(String args[])
         {
                char charArr = {"A","B","C"};
                String str = new String(charArr);
                StringBuilder sb;
                char ch;
                for(int i=0;i<charArr.length;i++)
                {
                         ch = str.charAt(0);
                         str = str.substring(1);
                         str = str + ch;
                         System.out.println(str);
                         sb = new StringBuilder(str);
                         sb = sb.reverse();
                         System.out.println(sb);
                  }
         }
}      
                        
                         

  Was this answer useful?  Yes

class jumble
{
public stativ void main(String aa[])
{
int a[]=[1,2,3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
a[i][j]=a[j][i];
}
System.out.println("the result is"+a[i][j]);
}

  Was this answer useful?  Yes

n_ulthi

  • Aug 5th, 2010
 

public class Shuffle {

    // swaps array elements i and j
    public static void swap(String[] a, int i, int j) {
        String swap = a[i];
        a[i] = a[j];
        a[j] = swap;
        swap = null;
    }

    // take as input an array of strings and rearrange them in random order
    public static void shuffle(String[] a) {
        int N = a.length;
        for (int i = 0; i < N; i++) {

            double random = Math.random();
            int r = i + (int) (random * (N - i)); // between i and N-1
            swap(a, i, r);
        }
    }

    // take as input an array of strings and print them out to standard output
    public static void show(String[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

    public static void main(String[] args) {

        String[] a = new String[] { new String("a"), new String("b"), new String("c"), new String("d"),
                new String("e"), new String("f"), new String("g"), new String("h"), new String("i"), new String("j") };

        // shuffle array and print permutation
        shuffle(a);
        show(a);

        System.out.println();

        // do it again
        shuffle(a);
        show(a);

    }
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions