Tuesday, February 21, 2017

DoTheShuffle

// ===============================================================
// The modern version of the Fisher–Yates shuffle, designed for
// computer use, was introduced by Richard Durstenfeld in 1964 and
// popularized by Donald E. Knuth in The Art of Computer
// Programming as "Algorithm P". Neither Durstenfeld nor Knuth, in
// the first edition of his book, acknowledged the work of Fisher
// and Yates; they may not have been aware of it. Subsequent
// editions of The Art of Computer Programming mention Fisher and
// Yates' contribution.
//
//                              - WIKIPEDIA (Fisher–Yates shuffle)
// ===============================================================

public class DoTheShuffle {

  private static void printSeq(String[] s) {
    for (int i = 0; i < s.length; i++) {
      if (i > 0) {
        System.out.print(" ");
      }
      System.out.print(s[i]);
    }
    System.out.println();
  }

  public static void main(String[] args) {

    ISort<String> sorter;
    IShuffle<String> shuffler;

    if (args.length == 0) {
      System.out.println("crickets");
      return;
    }

    sorter = new MeteredInsertionSorter<String>();
    shuffler = new LazyStudentShuffler<String>();

    // Original
    printSeq(args);

    // Sorted
    sorter.sort(args);
    printSeq(args);

    // Shuffled
    shuffler.shuffle(args);
    printSeq(args);
  }
}