|
| 1 | +/*********************************** |
| 2 | +* AUTHOR: Affan Khan |
| 3 | +* CREATE DATE: 2021-06-09 |
| 4 | +* PURPOSE: This Class extends from JPanel to create a JPanel that |
| 5 | + holds a list of randomly generated bars, and uses the heap Sort |
| 6 | + algorithm to sort through them. |
| 7 | +***********************************/ |
| 8 | +package src; |
| 9 | + |
| 10 | +import java.awt.*; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Random; |
| 14 | +import javax.swing.*; |
| 15 | + |
| 16 | + |
| 17 | +public class HeapSorter extends JPanel { |
| 18 | + //variables for bar graphics |
| 19 | + private final static int BAR_WIDTH = 30; |
| 20 | + private final static int BAR_HEIGHT = 400; |
| 21 | + private int[]list; |
| 22 | + //JPanel variable object to create/display and return |
| 23 | + private static JPanel mainPanel; |
| 24 | + |
| 25 | + //initializes the list of HeapSorter object |
| 26 | + private HeapSorter(int[] list){ |
| 27 | + this.list = list; |
| 28 | + } |
| 29 | + //sets the list to this HeapSorter object's list |
| 30 | + private void setItems(int[] list){ |
| 31 | + this.list = list; |
| 32 | + repaint(); |
| 33 | + } |
| 34 | + //sorts the HeapSorter object |
| 35 | + private void sort(){ |
| 36 | + new SortWorker(list).execute(); |
| 37 | + } |
| 38 | + |
| 39 | + /* |
| 40 | + * Paint component that creates the graphical bars |
| 41 | + */ |
| 42 | + @Override |
| 43 | + protected void paintComponent(Graphics g){ |
| 44 | + super.paintComponent(g); |
| 45 | + |
| 46 | + //creates the bar for each element |
| 47 | + for(int i = 0; i < list.length; i++){ |
| 48 | + int x = i * BAR_WIDTH; |
| 49 | + int y = getHeight() - list[i]; |
| 50 | + |
| 51 | + //makes the color red |
| 52 | + g.setColor( Color.RED ); |
| 53 | + g.fillRect(x, y, BAR_WIDTH, list[i]); |
| 54 | + g.setColor( Color.BLUE ); |
| 55 | + g.drawString("" + list[i], x, y); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + * Returns the Dimensions of the rectangle bars used for HeapSort graph |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public Dimension getPreferredSize(){ |
| 64 | + return new Dimension(list.length * BAR_WIDTH, BAR_HEIGHT + 20); |
| 65 | + } |
| 66 | + |
| 67 | + /* |
| 68 | + * Class that sorts the bars using the HeapSort algorithm |
| 69 | + */ |
| 70 | + private class SortWorker extends SwingWorker<Void, int[]>{ |
| 71 | + private int[] list; |
| 72 | + |
| 73 | + public SortWorker(int[] unsortedItems){ |
| 74 | + list = Arrays.copyOf(unsortedItems, unsortedItems.length); |
| 75 | + } |
| 76 | + |
| 77 | + //HeapSort algorithm |
| 78 | + //implementation of heap sort |
| 79 | + public void heapify(int list[], int n, int i){ |
| 80 | + int largest = i;//get largest as root |
| 81 | + int l = 2 * i + 1;//left = 2*i + 1 |
| 82 | + int r = 2 * i + 2;//right = 2*i + 2 |
| 83 | + |
| 84 | + //if left child is larger than root, replace it |
| 85 | + if(l < n && list[l] > list[largest]){ |
| 86 | + largest = l; |
| 87 | + } |
| 88 | + |
| 89 | + //if right child is larger than root, replace it |
| 90 | + if(r < n && list[r] > list[largest]){ |
| 91 | + largest = r; |
| 92 | + } |
| 93 | + |
| 94 | + //if largest is not root swap with element i |
| 95 | + if(largest != i){ |
| 96 | + int temp = list[i]; |
| 97 | + list[i] = list[largest]; |
| 98 | + list[largest] = temp; |
| 99 | + |
| 100 | + //repaint(); to show graphics to user |
| 101 | + publish( Arrays.copyOf(list, list.length) ); |
| 102 | + //sleep to slow down graphics |
| 103 | + try { Thread.sleep(100); } catch (Exception e) {} |
| 104 | + |
| 105 | + //recursivly call this method |
| 106 | + heapify(list, n, largest); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + //method to heapify a subtree |
| 111 | + @Override |
| 112 | + protected Void doInBackground(){ |
| 113 | + int n = list.length; |
| 114 | + |
| 115 | + //build heap |
| 116 | + for(int i = (n/2 - 1); i >= 0; i--){ |
| 117 | + heapify(list, n, i); |
| 118 | + } |
| 119 | + |
| 120 | + //now extract element from heap |
| 121 | + for(int i = n - 1; i > 0; i--){ |
| 122 | + //move current root to the end |
| 123 | + int temp = list[0]; |
| 124 | + list[0] = list[i]; |
| 125 | + list[i] = temp; |
| 126 | + |
| 127 | + //repaint(); to show graphics to user |
| 128 | + publish( Arrays.copyOf(list, list.length) ); |
| 129 | + //sleep to slow down graphics |
| 130 | + try { Thread.sleep(100); } catch (Exception e) {} |
| 131 | + |
| 132 | + //call max heapify on the reduced heap |
| 133 | + heapify(list, i, 0); |
| 134 | + } |
| 135 | + |
| 136 | + Toolkit tk = Toolkit.getDefaultToolkit(); |
| 137 | + tk.beep(); |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected void process(List<int[]> processList){ |
| 143 | + int[] list = processList.get(processList.size() - 1); |
| 144 | + setItems( list ); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void done() {} |
| 149 | + } |
| 150 | + |
| 151 | + //method that generates random numbers and fills an integer array |
| 152 | + private static int[]generateListNumbers(){ |
| 153 | + //variables initialized |
| 154 | + int[] list = new int[20]; |
| 155 | + |
| 156 | + /*creates random object and generates number from 0 to |
| 157 | + BAR_HEIGHT for each element*/ |
| 158 | + Random random = new Random(); |
| 159 | + for(int i = 0; i < list.length; i++){ |
| 160 | + list[i] = random.nextInt(HeapSorter.BAR_HEIGHT); |
| 161 | + } |
| 162 | + |
| 163 | + return list; |
| 164 | + } |
| 165 | + |
| 166 | + /* |
| 167 | + * method that will run the entire Heap Sorting algorithm. |
| 168 | + * From creating the objects and panels and buttons, to |
| 169 | + * calling all functions to sort, generate, order and return |
| 170 | + * a panel with a visual Heap Sorting algorithm. |
| 171 | + */ |
| 172 | + public static JPanel runHeapSort(){ |
| 173 | + //creates the object HeapSort, initializes the list, and fills it with randomly generated numbers |
| 174 | + HeapSorter heapSort = new HeapSorter(HeapSorter.generateListNumbers()); |
| 175 | + |
| 176 | + //adds title to the top of the panel |
| 177 | + JLabel title = new JLabel("Heap Sort"); |
| 178 | + |
| 179 | + //makes and adds two buttons, generate and sort |
| 180 | + JButton generate = new JButton("Generate Data"); |
| 181 | + generate.addActionListener((e)->heapSort.setItems(HeapSorter.generateListNumbers())); |
| 182 | + JButton sort = new JButton("Sort Data"); |
| 183 | + sort.addActionListener((e) -> heapSort.sort()); |
| 184 | + |
| 185 | + //adds the buttons to the panel |
| 186 | + JPanel bottomPanel = new JPanel(); |
| 187 | + bottomPanel.add(generate); |
| 188 | + bottomPanel.add(sort); |
| 189 | + |
| 190 | + //creates the mainPanel for the heapSort graphics |
| 191 | + mainPanel = new JPanel(); |
| 192 | + mainPanel.setLayout(new BorderLayout()); |
| 193 | + mainPanel.add(heapSort, BorderLayout.CENTER); |
| 194 | + mainPanel.add(bottomPanel, BorderLayout.SOUTH); |
| 195 | + mainPanel.add(title, BorderLayout.NORTH); |
| 196 | + |
| 197 | + return mainPanel; |
| 198 | + } |
| 199 | +} |
0 commit comments