Guest User

Untitled

a guest
Apr 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. // The "SortingAssignment" class.
  2. import java.awt.*;
  3. import hsa.Console;
  4.  
  5. public class SortingAssignment
  6. {
  7. static Console c; // The output console
  8. static int choice, i, j, temp, x, b, min, l, high, low;
  9. char y, n;
  10. public static void main (String[] args)
  11. {
  12. c = new Console ();
  13.  
  14. menu ();
  15.  
  16. }
  17.  
  18.  
  19. // Place your program here. 'c' is the output console
  20. // main method
  21. public static void menu ()
  22. {
  23.  
  24. c.println ("Please select a sorting method\n");
  25. c.println ("\t1.Insertion");
  26. c.println ("\t2.Selection");
  27. c.println ("\t3.Bubble");
  28. c.println ("\t4.Exit\n");
  29.  
  30. do
  31. {
  32. c.print ("What is your choice (1 - 4): ");
  33. choice = c.readInt ();
  34. if ((choice < 1) || (choice > 3))
  35. {
  36. c.println ("Invalid Number...enter 1-4 only!");
  37. }
  38. }
  39. while ((choice < 1) || (choice > 4));
  40.  
  41.  
  42. switch (choice)
  43. {
  44. case 1:
  45. c.println ("You chose option 1");
  46. Insertion ();
  47. break;
  48. case 2:
  49. c.println ("You chose option 2");
  50. Selection ();
  51. break;
  52. case 3:
  53. c.println ("You chose option 3");
  54. Bubble ();
  55. break;
  56. case 4:
  57. c.close ();
  58. break;
  59. }
  60.  
  61.  
  62.  
  63. }
  64.  
  65.  
  66. public static void Insertion ()
  67. {
  68. c.clear ();
  69. c.println ("Welcome to Insertion Sort");
  70.  
  71. c.println ("Enter the size of your Array");
  72. b = c.readInt ();
  73. c.println ("Enter the Lowest Number in the Array");
  74. low = c.readInt ();
  75. c.println ("Enter the Highest Number in the Array");
  76. high = c.readInt ();
  77. c.println ("Do you want the numbers to Descend? Y/N");
  78. choice = c.readInt ();
  79. if (choice == 'Y' || 'y')
  80. {
  81. while ((j < 0) && (number [j - 1] > temp))
  82. {
  83. number [j] = number [j - 1];
  84. j = j - 1;
  85. }
  86. }
  87. int number[] = new int [b];
  88.  
  89. for (j = 0 ; j < b ; j++)
  90. {
  91.  
  92. number [j] = (int) Math.ceil (Math.random () * (high - low)) + low;
  93.  
  94. }
  95. for (i = 1 ; i < number.length ; i++)
  96. {
  97. temp = number [i];
  98. j = i;
  99. while ((j > 0) && (number [j - 1] > temp))
  100. {
  101. number [j] = number [j - 1];
  102. j = j - 1;
  103. }
  104. number [j] = temp;
  105. }
  106. for (x = 0 ; x < number.length ; x++)
  107. {
  108. c.print (number [x], 7);
  109. }
  110. }
  111.  
  112.  
  113. public static void Selection ()
  114. {
  115. c.clear ();
  116. c.println ("Welcome to Selection Sort");
  117. c.println ("Enter the size of your Array");
  118. b = c.readInt ();
  119. c.println ("Enter the Lowest Number in the Array");
  120. low = c.readInt ();
  121. c.println ("Enter the Highest Number in the Array");
  122. high = c.readInt ();
  123. temp = 0;
  124. int number[] = new int [b];
  125. for (i = 0 ; 1 < number.length - 1 ; i++)
  126. {
  127. min = i;
  128. for (j = i + l ; j < number.length ; j++)
  129. {
  130. if (number [j] < number [min])
  131. min = j;
  132. }
  133. temp = number [i];
  134. number [i] = number [min];
  135. number [min] = temp;
  136. }
  137. c.print (number [x], 7);
  138. }
  139.  
  140.  
  141.  
  142. public static void Bubble ()
  143. {
  144. c.clear ();
  145. c.println ("Welcome to Bubble Sort");
  146. c.println ("Enter the size of your Array");
  147. b = c.readInt ();
  148.  
  149. c.println ("Enter the Lowest Number in the Array");
  150. low = c.readInt ();
  151. c.println ("Enter the Highest Number in the Array");
  152. high = c.readInt ();
  153. int number[] = new int [b];
  154. for (i = (number.length - 1) ; i >= 0 ; i--)
  155. {
  156. for (j = 1 ; j <= i ; j++)
  157. {
  158. if (number [j - 1] > number [j])
  159. {
  160. temp = number [j - 1];
  161. number [j - 1] = number [j];
  162. number [j] = temp;
  163. }
  164. }
  165. }
  166. }
  167. } // SortingAssignment class
Add Comment
Please, Sign In to add comment