GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithm-visualizer-java/SortingAlgorithm.java at main · Rustam-Z/algorithm-visualizer-java Expand file tree/ Copy pathSortingAlgorithm.javaMore file actions 221 lines (188 loc) · 7.9 KB / Copy pathSortingAlgorithm.javaFile metadata and controls221 lines (188 loc) · 7.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 // Implementation of Sorting algorithms import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.util.Random; import javax.swing.JPanel; import javax.swing.SwingWorker; public class SortingAlgorithm extends JPanel { private final int WIDTH = 800, HEIGHT = WIDTH * 9 / 16; public int SIZE = 100; // the number if sorting elements public float BAR_WIDTH = (float)WIDTH / SIZE; // bar width private float[] bar_height = new float[SIZE]; // height of bars private SwingWorker<Void, Void> shuffler, sorter; private int current_index, traversing_index; // needed for following colloring the items SortingAlgorithm() { setBackground(Color.BLACK); setPreferredSize(new Dimension(WIDTH, HEIGHT)); initBarHeight(); // initialize the height of each bar // initShuffler(); // shuffle each bar } // setter for SIZE public void setSIZE(int SIZE) { this.SIZE = SIZE; } // getter for SIZE int getSIZE() { return SIZE; } // repaint() will automaticly call this function // needed for coloring @Override public void paintComponent(Graphics g) { super.paintComponent(g); // Create randomizer Random random = new Random(); // Drawing the rectangles Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.CYAN); Rectangle2D.Float bar; for(int i = 0; i < getSIZE(); i++ ) { // random colors // final float hue = random.nextFloat(); // final float saturation = 0.9f; //1.0 for brilliant, 0.0 for dull // final float luminance = 1.0f; //1.0 for brighter, 0.0 for black // g2d.setColor(Color.getHSBColor(hue, saturation, luminance)); bar = new Rectangle2D.Float(i * BAR_WIDTH, 0, BAR_WIDTH-1, bar_height[i]); g2d.fill(bar); // g2d.draw(bar); } // Color setter for the current object g2d.setColor(Color.RED); bar = new Rectangle2D.Float(current_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[current_index]); g2d.fill(bar); // Color setter for the traversing object g2d.setColor(Color.YELLOW); bar = new Rectangle2D.Float(traversing_index * BAR_WIDTH, 0, BAR_WIDTH, bar_height[traversing_index]); g2d.fill(bar); } public void insertionSort() { /*Insertion sort algorithm*/ // Multithreading used for hadling the sorting sorter = new SwingWorker<>() { @Override public Void doInBackground() throws InterruptedException { // function for calling multithreading // Insertion sort algorithm starts for(current_index = 1; current_index < getSIZE(); current_index++) { traversing_index = current_index; while(traversing_index > 0 && bar_height[traversing_index] < bar_height[traversing_index - 1]) { swap(traversing_index, traversing_index - 1); traversing_index--; Thread.sleep(10); // controls the speed repaint(); // we need it because we ofter replace the contents of a JPanel } } current_index = 0; traversing_index = 0; return null; } }; } public void bubbleSort() { /*Bubble sorting algorithm*/ sorter = new SwingWorker<>() { @Override public Void doInBackground() throws InterruptedException { for(current_index = 0; current_index < getSIZE(); current_index++) { for(traversing_index = 1; traversing_index < (getSIZE() - current_index); traversing_index++) { if(bar_height[traversing_index - 1] > bar_height[traversing_index]) { swap(traversing_index, traversing_index - 1); traversing_index--; // just for annimation Thread.sleep(10); // controls the speed repaint(); // we need it because we ofter replace the contents of a JPanel } } } current_index = 0; traversing_index = 0; return null; } }; } public void mergeSort() { /*Merge sorting algorithm*/ // Change code from bubbleSort to mergeSort // TODO sorter = new SwingWorker<>() { @Override public Void doInBackground() throws InterruptedException { for(current_index = 0; current_index < getSIZE(); current_index++) { for(traversing_index = 1; traversing_index < (getSIZE() - current_index); traversing_index++) { if(bar_height[traversing_index - 1] > bar_height[traversing_index]) { swap(traversing_index, traversing_index - 1); traversing_index--; // just for annimation Thread.sleep(10); // controls the speed repaint(); // we need it because we ofter replace the contents of a JPanel } } } current_index = 0; traversing_index = 0; return null; } }; } public void selectionSort() { /*Merge sorting algorithm*/ // Change code from bubbleSort to mergeSort // TODO sorter = new SwingWorker<>() { @Override public Void doInBackground() throws InterruptedException { for(current_index = 0; current_index < getSIZE() - 1; current_index++) { int min_index = current_index; for(int traversing_index = current_index + 1; traversing_index < getSIZE(); traversing_index++) { if (bar_height[traversing_index] < bar_height[min_index]) { min_index = traversing_index; } } swap(current_index, min_index); Thread.sleep(10); // controls the speed repaint(); // we need it because we ofter replace the contents of a JPanel } current_index = 0; traversing_index = 0; return null; } }; } public void initShuffler() { /*Shuffles each bar*/ shuffler = new SwingWorker<>() { @Override public Void doInBackground() throws InterruptedException { int middle = getSIZE() / 2; for (int i = 0, j = middle; i < middle; i++, j++) { int randow_index = new Random().nextInt(getSIZE()); swap(i, randow_index); randow_index = new Random().nextInt(getSIZE()); swap(j, randow_index); Thread.sleep(10); // controls the speed repaint(); // we need it because we ofter replace the contents of a JPanel } return null; } // after finishing the process @Override public void done() { super.done(); sorter.execute(); } }; shuffler.execute(); } public void initBarHeight() { /*Initialize the height of each bar*/ float interval = (float)HEIGHT / getSIZE(); for(int i = 0; i < getSIZE(); i++) { bar_height[i] = i * interval; } } public void swap(int indexA, int indexB) { /*Swaps the elements*/ float temp = bar_height[indexA]; bar_height[indexA] = bar_height[indexB]; bar_height[indexB] = temp; } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |