|
| 1 | +package net.mechanika.sorting; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Graphics; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class RadixSortPanel extends SortPanel { |
| 9 | + private static final long serialVersionUID = 1L; |
| 10 | + private int redColumn = -1; |
| 11 | + private int blueColumn = -1; |
| 12 | + private int cyanColumn = -1; |
| 13 | + private int greenColumn = -1; |
| 14 | + final int RADIX = 10; |
| 15 | + |
| 16 | + public RadixSortPanel(String name, int sleepTime, int width, int height) { |
| 17 | + super(name, sleepTime, width, height); |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public void reset() { |
| 22 | + redColumn = -1; |
| 23 | + blueColumn = -1; |
| 24 | + greenColumn = -1; |
| 25 | + cyanColumn = -1; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void run() { |
| 30 | + try { |
| 31 | + |
| 32 | + boolean maxLength = false; |
| 33 | + int tmp = -1, placement = 1, digit; |
| 34 | + |
| 35 | + List<Integer>[] bucket = new ArrayList[RADIX]; |
| 36 | + for (int i = 0; i < bucket.length; i++) { |
| 37 | + bucket[i] = new ArrayList<Integer>(); |
| 38 | + } |
| 39 | + |
| 40 | + while (!maxLength) { |
| 41 | + maxLength = true; |
| 42 | + |
| 43 | + for (int i = 0; i < list.length; i++) { |
| 44 | + tmp = list[i] / placement; |
| 45 | + digit = tmp % 10; |
| 46 | + bucket[digit].add(list[i]); |
| 47 | + if (maxLength && tmp > 0) { |
| 48 | + maxLength = false; |
| 49 | + } |
| 50 | + // cyanColumn = (i % 10) == 0 ? i : -1; |
| 51 | + // repaint(); |
| 52 | + // Thread.sleep(2 * sleepTime); |
| 53 | + } |
| 54 | + |
| 55 | + // System.out.println("second loop"); |
| 56 | + |
| 57 | + int a = 0; |
| 58 | + for (int b = 0; b < RADIX; b++) { |
| 59 | + for (Integer i : bucket[b]) { |
| 60 | + // list[a++] = count[b][i]; |
| 61 | + redColumn = a; |
| 62 | + if (maxLength) { |
| 63 | + greenColumn = a; |
| 64 | + } |
| 65 | + list[a++] = i; |
| 66 | + repaint(); |
| 67 | + Thread.sleep(2 * sleepTime); |
| 68 | + } |
| 69 | + bucket[b].clear(); |
| 70 | + } |
| 71 | + // greenColumn = a; |
| 72 | + // greenColumn--; |
| 73 | + redColumn = -1; |
| 74 | + repaint(); |
| 75 | + Thread.sleep(4 * sleepTime); |
| 76 | + |
| 77 | + placement *= RADIX; |
| 78 | + } |
| 79 | + |
| 80 | + // for (int i = 0; i < list.length; i++) { |
| 81 | + // greenColumn = i; |
| 82 | + // repaint(); |
| 83 | + // Thread.sleep(sleepTime); |
| 84 | + // } |
| 85 | + redColumn = -1; |
| 86 | + cyanColumn = -1; |
| 87 | + } catch (InterruptedException e) { |
| 88 | + } |
| 89 | + repaint(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void paintComponent(Graphics g) { |
| 94 | + super.paintComponent(g); |
| 95 | + int columnWidth = (getWidth() - 4 * BORDER_WIDTH) / size; |
| 96 | + int columnHeight = (getHeight() - 4 * BORDER_WIDTH) / size; |
| 97 | + for (int i = (greenColumn == -1 ? 0 : greenColumn); i < list.length; i++) { |
| 98 | + g.setColor(Color.WHITE); |
| 99 | + g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight); |
| 100 | + g.setColor(Color.BLACK); |
| 101 | + g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight); |
| 102 | + } |
| 103 | + for (int i = 0; i <= greenColumn; i++) { |
| 104 | + g.setColor(Color.GREEN); |
| 105 | + g.fillRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight); |
| 106 | + g.setColor(Color.BLACK); |
| 107 | + g.drawRect(2 * BORDER_WIDTH + columnWidth * i, getHeight() - list[i] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[i] * columnHeight); |
| 108 | + } |
| 109 | + if(redColumn != -1) { |
| 110 | + g.setColor(Color.RED); |
| 111 | + g.fillRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight); |
| 112 | + g.setColor(Color.BLACK); |
| 113 | + g.drawRect(2 * BORDER_WIDTH + columnWidth * redColumn, getHeight() - list[redColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[redColumn] * columnHeight); |
| 114 | + } |
| 115 | + if(blueColumn != -1) { |
| 116 | + g.setColor(Color.BLUE); |
| 117 | + g.fillRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight); |
| 118 | + g.setColor(Color.BLACK); |
| 119 | + g.drawRect(2 * BORDER_WIDTH + columnWidth * blueColumn, getHeight() - list[blueColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[blueColumn] * columnHeight); |
| 120 | + } |
| 121 | + if(cyanColumn != -1) { |
| 122 | + g.setColor(Color.CYAN); |
| 123 | + g.fillRect(2 * BORDER_WIDTH + columnWidth * cyanColumn, getHeight() - list[cyanColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[cyanColumn] * columnHeight); |
| 124 | + g.setColor(Color.BLACK); |
| 125 | + g.drawRect(2 * BORDER_WIDTH + columnWidth * cyanColumn, getHeight() - list[cyanColumn] * columnHeight - 2 * BORDER_WIDTH, columnWidth, list[cyanColumn] * columnHeight); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments