fork download
  1. package sortingstuff;
  2.  
  3. // @Author Damien Bell
  4. import java.awt.Color;
  5. import javax.swing.JButton;
  6. import javax.swing.JLabel;
  7. import javax.swing.Timer;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import javax.swing.event.ChangeEvent;
  11. import javax.swing.event.ChangeListener;
  12. import javax.swing.JPanel;
  13. import javax.swing.JFrame;
  14. import javax.swing.SwingUtilities;
  15. import java.util.Random;
  16. import java.awt.GridLayout;
  17. import javax.swing.BorderFactory;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.lang.Thread;
  21.  
  22.  
  23. public class SortingStuff extends JFrame{
  24. public SortingStuff(){
  25. initUI();
  26. }
  27.  
  28. public static class thread2 implements Runnable{
  29. private String numbers[];
  30. private JButton numButton[];
  31.  
  32. public thread2(String[] numbers1, JButton[] numButton1){
  33. numbers = numbers1;
  34. numButton = numButton1;
  35. }
  36. public void doSort(String[] numbers, JButton[] numButton){
  37. System.out.println(Thread.currentThread().getName());
  38. Sort.bubbleSort(numbers, numButton);
  39. }
  40.  
  41. public void run() {
  42. doSort(numbers, numButton);
  43. }
  44. }
  45.  
  46. JButton numButton[] = new JButton[12];
  47. String numbers[] = new String[12];
  48. JLabel spacer[]= new JLabel[10];
  49. JButton Bubble = new JButton();
  50. JButton Merge = new JButton();
  51. JButton Insertion = new JButton();
  52. JButton Heap = new JButton();
  53. JButton MergeSort = new JButton();
  54. JButton Randomize = new JButton();
  55.  
  56. public final void genRandom(String strArray[]){
  57. Random generator = new Random();
  58. for(int i=0; i<strArray.length; i++){
  59. strArray[i] = Integer.toString(Math.abs(generator.nextInt()%100));
  60. }
  61. }
  62.  
  63. public final void genReRandom(String numbers[], JButton numButton[]){
  64. Random generator = new Random();
  65. for(int i=0; i<numButton.length; i++){
  66. String str1=Integer.toString(Math.abs(generator.nextInt()%100));
  67. numButton[i].setText(str1);
  68. numbers[i]=str1;
  69. }
  70. }
  71.  
  72. public final void initUI(){
  73. JPanel panel = new JPanel();
  74. getContentPane().add(panel);
  75. panel.setBorder(BorderFactory.createEmptyBorder(2,12,4,5));
  76. panel.setLayout(new GridLayout(2,12,4,5));
  77.  
  78. genRandom(numbers);
  79. System.out.println(Thread.currentThread().getName());
  80. for(int i=0; i<numbers.length; i++){
  81. numButton[i] = new JButton();
  82. numButton[i].setText(numbers[i]);
  83. panel.add(numButton[i]);
  84. }
  85. for(int i=0; i<3; i++){
  86. spacer[i] = new JLabel();
  87. spacer[i].setText("");
  88. panel.add(spacer[i]);
  89. }
  90.  
  91. Bubble.setText("<html>&nbsp;Bubble <br> <center>Sort</center></html>");
  92. Bubble.addActionListener(new ActionListener(){
  93. public void actionPerformed(ActionEvent ae){
  94. System.out.println("thread " +Thread.currentThread().getName());
  95. thread2 myThread2 = new thread2(numbers, numButton);
  96. new Thread(myThread2).start();
  97. }
  98. });
  99. panel.add(Bubble);
  100.  
  101. Merge.setText("<html>&nbsp;Merge <br> <center>Sort</center></html>");
  102. Merge.addActionListener(new ActionListener(){
  103. public void actionPerformed(ActionEvent ae){
  104. Sort.bubbleSort(numbers, numButton);
  105. }
  106. });
  107. panel.add(Merge);
  108.  
  109. Insertion.setText("<html>&nbsp;Insertion<br> <center>Sort</center></html>");
  110. Insertion.addActionListener(new ActionListener(){
  111. public void actionPerformed(ActionEvent ae){
  112. Sort.bubbleSort(numbers, numButton);
  113. }
  114. });
  115. panel.add(Insertion);
  116.  
  117. Heap.setText("<html>&nbsp;Heap <br> <center>Sort</center></html>");
  118. Heap.addActionListener(new ActionListener(){
  119. public void actionPerformed(ActionEvent ae){
  120. Sort.bubbleSort(numbers, numButton);
  121. }
  122. });
  123. panel.add(Heap);
  124.  
  125. Randomize.setText("Randomize");
  126. Randomize.addActionListener(new ActionListener(){
  127. public void actionPerformed(ActionEvent ae){
  128. genReRandom(numbers, numButton);
  129. }
  130. });
  131. panel.add(Randomize);
  132.  
  133. for(int i=4; i<7; i++){
  134. spacer[i] = new JLabel();
  135. spacer[i].setText("");
  136. panel.add(spacer[i]);
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. add(panel);
  144. setTitle("Test");
  145. setSize(1600,300);
  146. setLocationRelativeTo(null);
  147. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  148.  
  149. }
  150.  
  151. public static void main(String[] args){
  152. SwingUtilities.invokeLater(new Runnable() {
  153. public void run(){
  154. SortingStuff ss = new SortingStuff();
  155. ss.setVisible(true);
  156. }
  157. });
  158. }
  159. }
  160.  
  161.  
  162. /////////////////////////////////////////////////////////////////
  163.  
  164. package sortingstuff;
  165.  
  166. import java.awt.Color;
  167. import javax.swing.JButton;
  168.  
  169. public class Sort {
  170. public static void bubbleSort(final String numbers[], final JButton numButton[]){
  171. double startTime = System.nanoTime();
  172. System.out.println("thread " +Thread.currentThread().getName());
  173. for (int k = 0; k < numbers.length - 1; k++){
  174. boolean isSorted = true;
  175. int delay500=500, delay750=750, delay2000=1500;
  176. for (int i = 1; i < numbers.length - k; i++) {
  177. numButton[i].setBackground(Color.YELLOW);
  178. numButton[i-1].setBackground(Color.YELLOW);
  179. try{
  180. delay500 = doSlow.doSlow250(delay500);
  181. Thread.sleep(delay500);
  182. }catch(Exception e){ System.err.println("Error: " + e.getMessage());}
  183. if (Integer.parseInt(numbers[i]) < Integer.parseInt(numbers[i - 1]) ){
  184.  
  185. isSorted = false;
  186. try{
  187. String tempVariable = numbers[i];
  188. numbers[i] = numbers[i - 1];
  189. numbers[i - 1] = tempVariable;
  190. String str1 = numButton[i].getText();
  191. numButton[i].setBackground(Color.RED);
  192. numButton[i-1].setBackground(Color.RED);
  193. delay2000 = doSlow.doSlow250(delay2000);
  194. Thread.sleep(delay2000);
  195. numButton[i].setText(numButton[i-1].getText());
  196. numButton[i-1].setText(str1);
  197. numButton[i].setBackground(Color.GREEN);
  198. numButton[i-1].setBackground(Color.GREEN);
  199. delay750 = doSlow.doSlow250(delay2000);
  200. Thread.sleep(delay750);
  201. numButton[i].setBackground(null);
  202. numButton[i-1].setBackground(null);
  203. }catch(Exception e){ System.err.println("Error: " + e.getMessage());}
  204. }
  205. numButton[i].setBackground(null);
  206. numButton[i-1].setBackground(null);
  207. }
  208.  
  209. if (isSorted){
  210. double endTime = System.nanoTime();
  211. double elapsedTime = endTime - startTime;
  212. System.out.println("This operation took " + elapsedTime + " nanoseconds, which is :" + ((elapsedTime/1000000)/1000) + " seconds.");
  213. break;
  214. }
  215. }
  216. }
  217. }
  218.  
  219. ///////////////////////////////////////
  220.  
  221.  
  222. package sortingstuff;
  223. public class doSlow {
  224.  
  225. public static int doSlow250(int x){
  226. return x;
  227. }
  228.  
  229. }
  230.  
  231.  
  232. /////////////////////////////////////
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:164: class, interface, or enum expected
package sortingstuff;
^
Main.java:166: class, interface, or enum expected
import java.awt.Color;
^
Main.java:167: class, interface, or enum expected
import javax.swing.JButton;
^
Main.java:222: class, interface, or enum expected
package sortingstuff;
^
4 errors
stdout
Standard output is empty