'How can I improve the amount of time my GUI takes to load quick sort?
I have finished coding my Quick Sort algorithm and linked it to my GUI program to visualize it. The problem is that it takes a significant amount of time to draw. Other sorts that are a part of my program utilize the Swing timer built into the launchSort() method. However, because Quick Sort uses recursion, I could not (or at least figure out how) to allow the GUI to use the Swing timer to draw the graph one sort at a time. I discovered a solution that constructs a timer in the Quick Sort program, however I find the graph to draw too slow.
Quick Sort Code
public QuickSort(int[] arr) {
array = arr;
left = 0;
right = arr.length - 1;
timer = new Timer();
}
@Override
public boolean sortStep() {
Drawer drawer = new Drawer();
Graphics g = drawer.getGraphics();
quickSorting(array, left, right, drawer, g);
return false;
}
public void quickSorting(int array[], int left, int right, Drawer drawer, Graphics g) {
if (left < right) {
int pivot = partition(array, left, right, drawer, g);
quickSorting(array, left, pivot - 1, drawer, g);
quickSorting(array, pivot + 1, right, drawer, g);
}
}
public int partition(int[] array, int low, int high, Drawer drawer, Graphics g) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (array[j] < pivot) {
i++;
swap(array, i ,j, drawer, g);
}
}
swap(array, i + 1, high, drawer, g);
return (i + 1);
}
public void swap(int[] array, int i, int j, Drawer drawer, Graphics g) {
TimerTask t = new TimerTask() {
public void run() {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
};
timer.schedule(t, 1);
drawer.repaint();
}
}
GUI Code
public class Drawer extends JPanel implements Runnable {
private JComboBox<sortExec.sortMethod> sortAlgorithmsCombo;
private JFrame frame;
public JButton sortButton;
private JButton randomizeButton;
private Timer timer;
private int[] newArray;
private boolean randSelected;
private boolean stopSorting;
public Drawer() {
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
setBackground(Color.BLACK);
setOpaque(false);
}
@Override
public void run() {
createAndDisplayGui();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int[] y = sortExec.getArray();
if (y != null) {
for (int i = 0; i < y.length; i++) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(i, 0, 5, y[i]);
g2d.setColor(Color.BLACK);
g2d.fillRect(i, 0, 5, y[i]);
}
}
}
private void createAndDisplayGui() {
frame = new JFrame("Sort Visualizer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTopPanel(), BorderLayout.PAGE_START);
frame.add(this, BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.setSize(550, 650);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
sortButton = new JButton("Sort");
sortButton.setMnemonic(KeyEvent.VK_S);
sortButton.addActionListener(this::launchSort);
buttonsPanel.add(sortButton);
randomizeButton = new JButton("Randomize");
randomizeButton.setMnemonic(KeyEvent.VK_S);
randomizeButton.addActionListener(this::launchSort);
buttonsPanel.add(randomizeButton);
return buttonsPanel;
}
private JPanel createTopPanel() {
JPanel topPanel = new JPanel();
JLabel label = new JLabel("Sort Algorithm");
topPanel.add(label);
sortAlgorithmsCombo = new JComboBox<>(sortExec.sortMethod.values());
topPanel.add(sortAlgorithmsCombo);
return topPanel;
}
private void launchSort(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Sort")) {
sortButton.setEnabled(false);
randomizeButton.setEnabled(false);
}
if(command.equals("Randomize")) {
randSelected = true;
newArray = sortExec.arrayGenerator();
}
sortExec.sortMethod requestedSort = (sortExec.sortMethod) sortAlgorithmsCombo.getSelectedItem();
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
if (randSelected == false) {
sortExec.initSort(requestedSort);
}
else if (randSelected == true) {
sortExec.randomizedInitSort(newArray, requestedSort);
}
}
catch (RuntimeException xRuntime) {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
throw xRuntime;
}
if (command.equals("Sort")){// && sortAlgorithmsCombo.getSelectedItem().equals(requestedSort))
timer = new Timer(50, this::performSort);
timer.setInitialDelay(0);
timer.start();
}
else if (command.equals("Randomize")) {
repaint();
}
}
private void performSort(ActionEvent event) {
stopSorting = sortExec.performSort();
repaint();
if (stopSorting) {
timer.stop();
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
JOptionPane.showMessageDialog(frame,
"Sorting completed.",
"Complete",
JOptionPane.INFORMATION_MESSAGE);
sortButton.setEnabled(true);
randomizeButton.setEnabled(true);
randSelected = false;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Drawer());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
