import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.*;
public class Test
{
public JFrame frame;
public JPanel buttonPanel;
public JPanel arrayPanel;
public JPanel bottomPanel;
public JLabel label;
public JTextField t1;
public JTextField t2;
public JTextField t3;
public JTextField t4;
public JTextField t5;
public JTextField t6;
public JTextField t7;
public JTextField t8;
public JButton button;
public JButton button2;
public int[] numbers = new int[8];
public Test()
{
frame = new JFrame("Quicksort");
this.buildButtonPanel();
frame.add(buttonPanel, BorderLayout.NORTH);
this.buildArrayPanel();
frame.add(arrayPanel, BorderLayout.CENTER);
this.buildBottomPanel();
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void buildButtonPanel()
{
button = new JButton("Generate Numbers");
button2 = new JButton("Quicksort Numbers");
button.addActionListener(new ButtonListener());
buttonPanel = new JPanel();
buttonPanel.add(button);
buttonPanel.add(button2);
}
private void buildArrayPanel()
{
t1 = new JTextField("",2);
t2 = new JTextField("",2);
t3 = new JTextField("",2);
t4 = new JTextField("",2);
t5 = new JTextField("",2);
t6 = new JTextField("",2);
t7 = new JTextField("",2);
t8 = new JTextField("",2);
arrayPanel = new JPanel();
arrayPanel.add(t1);
arrayPanel.add(t2);
arrayPanel.add(t3);
arrayPanel.add(t4);
arrayPanel.add(t5);
arrayPanel.add(t6);
arrayPanel.add(t7);
arrayPanel.add(t8);
}
public void buildBottomPanel()
{
bottomPanel = new JPanel();
label = new JLabel("Ugh...");
label.setPreferredSize(new Dimension(6,6));
label.setText("Ugh...");
bottomPanel.add(label);
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button)
{
//Variables
int num1 = (int)(Math.random()*100);
int num2 = (int)(Math.random()*100);
int num3 = (int)(Math.random()*100);
int num4 = (int)(Math.random()*100);
int num5 = (int)(Math.random()*100);
int num6 = (int)(Math.random()*100);
int num7 = (int)(Math.random()*100);
int num8 = (int)(Math.random()*100);
//Show array on screen
t1.setText(Integer.toString(num1));
t2.setText(Integer.toString(num2));
t3.setText(Integer.toString(num3));
t4.setText(Integer.toString(num4));
t5.setText(Integer.toString(num5));
t6.setText(Integer.toString(num6));
t7.setText(Integer.toString(num7));
t8.setText(Integer.toString(num8));
//Copy numbers into array
numbers[0] = Integer.parseInt(t1.getText());
numbers[1] = Integer.parseInt(t2.getText());
numbers[2] = Integer.parseInt(t3.getText());
numbers[3] = Integer.parseInt(t4.getText());
numbers[4] = Integer.parseInt(t5.getText());
numbers[5] = Integer.parseInt(t6.getText());
numbers[6] = Integer.parseInt(t7.getText());
numbers[7] = Integer.parseInt(t8.getText());
}
if(e.getSource() == button2)
{
quickSort(numbers, 0, 7);
t1.setText(Integer.toString(numbers[0]));
t2.setText(Integer.toString(numbers[1]));
t3.setText(Integer.toString(numbers[2]));
t4.setText(Integer.toString(numbers[3]));
t5.setText(Integer.toString(numbers[4]));
t6.setText(Integer.toString(numbers[5]));
t7.setText(Integer.toString(numbers[6]));
t8.setText(Integer.toString(numbers[7]));
}
}
}
public void quickSort(int array[], int start, int end)
{
int i = start;
int k = end;
if (end - start >= 1)
{
int pivot = array[start];
while (k > i)
{
while (array[i] <= pivot && i <= end && k > i)
i++;
while (array[k] > pivot && k >= start && k >= i)
k--;
if (k > i)
swap(array, i, k);
}
swap(array, start, k);
quickSort(array, start, k - 1);
quickSort(array, k + 1, end);
}
else {
return;
}
}
public void swap(int array[], int index1, int index2)
{
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
public static void main(String[] args)
{
Test test = new Test();
}
}