Lab 5 - Permutation Generator

JAVA 8 API

Lab Setup

Outline (Part 1)

Create a class to generate permutation of consecutive integers.
For example, 1,2,3,4 are consecutive integers. 2,4,3,1 is a permutation of those integers (a rearrangement).

The PermutationGenerator should generate permutations for any range of numbers. Fill an ArrayList, itsNumbers, with the Integers low to high in the constructor (sequentially, in order).

The method nextPermutation returns a list of the itsNumbers in random order with no repetitions. To do this create a new list and remove each number randomly from the original list and append it to the new list. The list element to be removed is selected randomly. Be sure to replace the instance variable holding the ArrayList for the class with the permutation before returning it.

PermutationGenerator pg = new PermutationGenerator(1, 52);
print(pg.nextPermutation(),13); // only show 13 per line


Use the above to test your class and methods. (This shuffles a deck of 52 cards).

PART 2
Run an experiment to calculate the number of times a list has to be permutated to be back in order. Write static methods for PermutationTest with these headers:

/** returns true if the list is in increasing order */
public static boolean inorder(ArrayList list)

/** returns the average number of permutations necessary to put a list in order. Do at least (3,100), (4,100), (5,100) */
public static int runExperiment(int lengthOfList, int times)