Guest User

Untitled

a guest
Apr 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 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. static char choice1, 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? enter Y/N");
  78. choice1 = c.readChar ();
  79. int number[] = new int [b];
  80.  
  81.  
  82. if (choice1 == y)
  83. {
  84. for (j = 0 ; j < b ; j++)
  85. {
  86.  
  87. number [j] = (int) Math.ceil (Math.random () * (high - low)) + low;
  88.  
  89. }
  90. for (i = 1 ; i < number.length ; i++)
  91. {
  92. temp = number [i];
  93. j = i;
  94. while ((j > 0) && (number [j - 1] > temp))
  95. {
  96. number [j] = number [j - 1];
  97. j = j - 1;
  98. }
  99. number [j] = temp;
  100. }
  101.  
  102.  
  103. for (x = 0 ; x < number.length ; x++)
  104. {
  105. c.print (number [x], 7);
  106. }
  107. }
  108.  
  109. if (choice1 == n)
  110. {
  111. for (j = 0 ; j < b ; j++)
  112. {
  113.  
  114. number [j] = (int) Math.ceil (Math.random () * (high - low)) + low;
  115.  
  116. }
  117. for (i = 1 ; i < number.length ; i++)
  118. {
  119. temp = number [i];
  120. j = i;
  121. while ((j > 0) && (number [j - 1] < temp))
  122. {
  123. number [j] = number [j - 1];
  124. j = j - 1;
  125. }
  126. number [j] = temp;
  127. }
  128.  
  129.  
  130. for (x = 0 ; x < number.length ; x++)
  131. {
  132. c.print (number [x], 7);
  133. }
  134. }
  135. }
  136.  
  137.  
  138. public static void Selection ()
  139. {
  140. c.clear ();
  141. c.println ("Welcome to Selection Sort");
  142. c.println ("Enter the size of your Array");
  143. b = c.readInt ();
  144. c.println ("Enter the Lowest Number in the Array");
  145. low = c.readInt ();
  146. c.println ("Enter the Highest Number in the Array");
  147. high = c.readInt ();
  148. temp = 0;
  149. int number[] = new int [b];
  150. for (i = 0 ; 1 < number.length - 1 ; i++)
  151. {
  152. min = i;
  153. for (j = i + l ; j < number.length ; j++)
  154. {
  155. if (number [j] < number [min])
  156. min = j;
  157. }
  158. number[j]= number [i];
  159. number [i] = number [min];
  160. number [min] = temp;
  161. }
  162.  
  163.  
  164. c.print (number [x], 7);
  165. }
  166.  
  167.  
  168.  
  169. public static void Bubble ()
  170. {
  171. c.clear ();
  172. c.println ("Welcome to Bubble Sort");
  173. c.println ("Enter the size of your Array");
  174. b = c.readInt ();
  175.  
  176. c.println ("Enter the Lowest Number in the Array");
  177. low = c.readInt ();
  178. c.println ("Enter the Highest Number in the Array");
  179. high = c.readInt ();
  180. int number[] = new int [b];
  181. for (i = (number.length - 1) ; i >= 0 ; i--)
  182. {
  183. for (j = 1 ; j <= i ; j++)
  184. {
  185. if (number [j - 1] > number [j])
  186. {
  187. temp = number [j - 1];
  188. number [j - 1] = number [j];
  189. number [j] = temp;
  190. }
  191. }
  192. }
  193. }
  194. } // SortingAssignment class
Add Comment
Please, Sign In to add comment