Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the purpose is just to generate a random permutation, I don't really understand the need for sorting. The following code runs in linear time as far as I can tell</p> <pre><code>public static int[] getRandomPermutation (int length){ // initialize array and fill it with {0,1,2...} int[] array = new int[length]; for(int i = 0; i &lt; array.length; i++) array[i] = i; for(int i = 0; i &lt; length; i++){ // randomly chosen position in array whose element // will be swapped with the element in position i // note that when i = 0, any position can chosen (0 thru length-1) // when i = 1, only positions 1 through length -1 // NOTE: r is an instance of java.util.Random int ran = i + r.nextInt (length-i); // perform swap int temp = array[i]; array[i] = array[ran]; array[ran] = temp; } return array; } </code></pre> <p>And here is some code to test it:</p> <pre><code>public static void testGetRandomPermutation () { int length =4; // length of arrays to construct // This code tests the DISTRIBUTIONAL PROPERTIES ArrayList&lt;Integer&gt; counts = new ArrayList &lt;Integer&gt; (); // filled with Integer ArrayList&lt;int[]&gt; arrays = new ArrayList &lt;int[]&gt; (); // filled with int[] int T = 1000000; // number of trials for (int t = 0; t &lt; T; t++) { int[] perm = getRandomPermutation(length); // System.out.println (getString (perm)); boolean matchFound = false; for(int j = 0; j &lt; arrays.size(); j++) { if(equals(perm,arrays.get(j))) { //System.out.println ("match found!"); matchFound = true; // increment value of count in corresponding position of count list counts.set(j, Integer.valueOf(counts.get(j).intValue()+1)); break; } } if (!matchFound) { arrays.add(perm); counts.add(Integer.valueOf(1)); } } for(int i = 0; i &lt; arrays.size(); i++){ System.out.println (getString (arrays.get (i))); System.out.println ("frequency: " + counts.get (i).intValue ()); } // Now let's test the speed T = 500000; // trials per array length n // n will the the length of the arrays double[] times = new double[97]; for(int n = 3; n &lt; 100; n++){ long beginTime = System.currentTimeMillis(); for(int t = 0; t &lt; T; t++){ int[] perm = getRandomPermutation(n); } long endTime = System.currentTimeMillis(); times[n-3] = (double)(endTime-beginTime); System.out.println("time to make "+T+" random permutations of length "+n+" : "+ (endTime-beginTime)); } // Plotter.plot(new double[][]{times}); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload