aznishboy

SortStep

Dec 16th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.85 KB | None | 0 0
  1. http://pastebin.com/iuF9fExJ
  2. import chn.util.*;
  3. import apcslib.Format;
  4. import java.util.*;
  5.  
  6. /**
  7.  *  Driver program for the Sorts class.
  8.  *
  9.  * @author     G. Peck
  10.  * @created    July 18, 2002
  11.  */
  12. public class SortStep
  13. {
  14.   private ConsoleIO console;
  15.   private int[] myArray;
  16.   private Sorts mySorts;
  17.  
  18.   /**
  19.    *  Constructor for the SortStep object
  20.    */
  21.   public SortStep()
  22.   {
  23.     console = new ConsoleIO();
  24.     mySorts = new Sorts();
  25.   }
  26.  
  27.   /**
  28.    *  Asks the user to select a sorting algorithm, fills the array
  29.    *  with an amount of random integer data chosen by the user, calls
  30.    *  the sorting algorithm, and gives an option of printing out the
  31.    *  data after it has been sorted.
  32.    */
  33.   public void sortMenu()
  34.   {
  35.     String choice;
  36.     String print;
  37.  
  38.     do
  39.     {
  40.       System.out.println();
  41.       System.out.println("Sorting algorithm menu");
  42.       System.out.println();
  43.       System.out.println("(1) Bubble sort");
  44.       System.out.println("(2) Selection sort");
  45.       System.out.println("(3) Insertion sort");
  46.       System.out.println("(4) Recursive mergesort");
  47.       System.out.println("(5) Quicksort");
  48.       System.out.println("(Q) Quit");
  49.       System.out.println();
  50.       System.out.print("Choice ---> ");
  51.       choice = console.readLine() + " ";
  52.       if ('1' <= choice.charAt(0) && choice.charAt(0) <= '5')
  53.       {
  54.         System.out.println();
  55.         System.out.print("How many numbers do you wish to generate? ");
  56.         int numInts = console.readInt();
  57.         System.out.print("Largest integer to generate? ");
  58.         int largestInt = console.readInt();
  59.         fillArray(numInts, largestInt);
  60.  
  61.         mySorts.setStepCount(0);
  62.  
  63.         switch (choice.charAt(0))
  64.         {
  65.             case '1':
  66.               mySorts.bubbleSort(myArray);
  67.               break;
  68.             case '2':
  69.               mySorts.selectionSort(myArray);
  70.               break;
  71.             case '3':
  72.               mySorts.insertionSort(myArray);
  73.               break;
  74.             case '4':
  75.               mySorts.mergeSort(myArray, 0, myArray.length - 1);
  76.               break;
  77.             case '5':
  78.               mySorts.quickSort(myArray, 0, myArray.length - 1);
  79.               break;
  80.         }
  81.         System.out.println();
  82.         System.out.print("Print list to screen (y/n)? ");
  83.         print = console.readLine();
  84.         if (print.charAt(0) == 'y' || print.charAt(0) == 'Y')
  85.         {
  86.           screenOutput();
  87.         }
  88.         System.out.println();
  89.         System.out.println("# steps = " + mySorts.getStepCount());
  90.         System.out.println();
  91.         System.out.print("Hit return to continue");
  92.         console.readLine();
  93.       }
  94.     } while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
  95.   }
  96.  
  97.   /**
  98.    *  Initializes myArray with random integers in the range
  99.    *  1..largestInt
  100.    *
  101.    * @param  numInts     number of integers to generate (size of
  102.    *      myArray)
  103.    * @param  largestInt  largest possible random integer to create
  104.    */
  105.   private void fillArray(int numInts, int largestInt)
  106.   {
  107.     myArray = new int[numInts];
  108.     Random randGen = new Random();
  109.  
  110.     for (int loop = 0; loop < myArray.length; loop++)
  111.     {
  112.       myArray[loop] = randGen.nextInt(largestInt) + 1;
  113.     }
  114.   }
  115.  
  116.   /**
  117.    *  prints out the contents of the array in tabular form, 12 columns
  118.    */
  119.   private void screenOutput()
  120.   {
  121.     for (int loop = 0; loop < myArray.length; loop++)
  122.     {
  123.       if (loop % 12 == 0)
  124.       {
  125.         System.out.println();
  126.       }
  127.       System.out.print(Format.right(myArray[loop], 6));
  128.     }
  129.     System.out.println();
  130.   }
  131.  
  132.   /**
  133.    *  Provides a main method for access to the sorting menu
  134.    *
  135.    * @param  args  The command line arguments (not used)
  136.    */
  137.   public static void main(String[] args)
  138.   {
  139.     SortStep doSorts = new SortStep();
  140.  
  141.     doSorts.sortMenu();
  142.   }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment