Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The "SortingAssignment" class.
- import java.awt.*;
- import hsa.Console;
- public class SortingAssignment
- {
- static Console c; // The output console
- static int choice, i, j, temp, x, b, min, l, high, low;
- char y, n;
- public static void main (String[] args)
- {
- c = new Console ();
- menu ();
- }
- // Place your program here. 'c' is the output console
- // main method
- public static void menu ()
- {
- c.println ("Please select a sorting method\n");
- c.println ("\t1.Insertion");
- c.println ("\t2.Selection");
- c.println ("\t3.Bubble");
- c.println ("\t4.Exit\n");
- do
- {
- c.print ("What is your choice (1 - 4): ");
- choice = c.readInt ();
- if ((choice < 1) || (choice > 3))
- {
- c.println ("Invalid Number...enter 1-4 only!");
- }
- }
- while ((choice < 1) || (choice > 4));
- switch (choice)
- {
- case 1:
- c.println ("You chose option 1");
- Insertion ();
- break;
- case 2:
- c.println ("You chose option 2");
- Selection ();
- break;
- case 3:
- c.println ("You chose option 3");
- Bubble ();
- break;
- case 4:
- c.close ();
- break;
- }
- }
- public static void Insertion ()
- {
- c.clear ();
- c.println ("Welcome to Insertion Sort");
- c.println ("Enter the size of your Array");
- b = c.readInt ();
- c.println ("Enter the Lowest Number in the Array");
- low = c.readInt ();
- c.println ("Enter the Highest Number in the Array");
- high = c.readInt ();
- c.println ("Do you want the numbers to Descend? Y/N");
- choice = c.readInt ();
- if (choice == 'Y' || 'y')
- {
- while ((j < 0) && (number [j - 1] > temp))
- {
- number [j] = number [j - 1];
- j = j - 1;
- }
- }
- int number[] = new int [b];
- for (j = 0 ; j < b ; j++)
- {
- number [j] = (int) Math.ceil (Math.random () * (high - low)) + low;
- }
- for (i = 1 ; i < number.length ; i++)
- {
- temp = number [i];
- j = i;
- while ((j > 0) && (number [j - 1] > temp))
- {
- number [j] = number [j - 1];
- j = j - 1;
- }
- number [j] = temp;
- }
- for (x = 0 ; x < number.length ; x++)
- {
- c.print (number [x], 7);
- }
- }
- public static void Selection ()
- {
- c.clear ();
- c.println ("Welcome to Selection Sort");
- c.println ("Enter the size of your Array");
- b = c.readInt ();
- c.println ("Enter the Lowest Number in the Array");
- low = c.readInt ();
- c.println ("Enter the Highest Number in the Array");
- high = c.readInt ();
- temp = 0;
- int number[] = new int [b];
- for (i = 0 ; 1 < number.length - 1 ; i++)
- {
- min = i;
- for (j = i + l ; j < number.length ; j++)
- {
- if (number [j] < number [min])
- min = j;
- }
- temp = number [i];
- number [i] = number [min];
- number [min] = temp;
- }
- c.print (number [x], 7);
- }
- public static void Bubble ()
- {
- c.clear ();
- c.println ("Welcome to Bubble Sort");
- c.println ("Enter the size of your Array");
- b = c.readInt ();
- c.println ("Enter the Lowest Number in the Array");
- low = c.readInt ();
- c.println ("Enter the Highest Number in the Array");
- high = c.readInt ();
- int number[] = new int [b];
- for (i = (number.length - 1) ; i >= 0 ; i--)
- {
- for (j = 1 ; j <= i ; j++)
- {
- if (number [j - 1] > number [j])
- {
- temp = number [j - 1];
- number [j - 1] = number [j];
- number [j] = temp;
- }
- }
- }
- }
- } // SortingAssignment class
Add Comment
Please, Sign In to add comment