***PLEASE MAKE SURE THE CODE RUNS IN NETBEANS JAVA SE*** (Bubble sort animation)
ID: 3832585 • Letter: #
Question
***PLEASE MAKE SURE THE CODE RUNS IN NETBEANS JAVA SE***
(Bubble sort animation) write a program that animates the bubble sort algorithm. Create an array that consists of 20 distinct numbers from 1 to 20 in a random order. The array elements are displayed as shown Figure 23.20 lb. Clicking the Step button causes the program to perform one comparison in the algorithm and repaints the histogram for the new array. Color the bar that represents the number being considered in the swap, When the algorithm is finished, display a message to inform the user. Clicking the Reset button creates a new random array for a new start. (Radix sort animation) Write a program that animates the radix sort algorithm. Create an array that consists of 20 random numbers from 0 to 1,000. The array elements are displayed, as shown in Figure 23.21. Clicking the Step button causes the program to place a number in a bucket. The number that has just been placed is displayed in red. Once all the numbers are placed in the buckets. clicking the Step button collects all the numbers from the buckets and moves them back to the array. When the algorithm is finished, clicking the Step button displays a message to inform the user. Clicking the Reset button creates a new random array for a new start.Explanation / Answer
Exercise16.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
public class Exercise16 extends JApplet {
private static final long serialVersionUID = 1L;
private BubbleSortPanel bubbleSortPanel = new BubbleSortPanel();
private JButton jButton1 = new JButton("Step");
public Exercise16() {
setLayout(new BorderLayout());
add(bubbleSortPanel, BorderLayout.CENTER);
JPanel panel1 = new JPanel();
panel1.add(jButton1);
JButton jButton2 = new JButton("Reset");
panel1.add(jButton2);
add(panel1, BorderLayout.SOUTH);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bubbleSortPanel.nextStep();
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bubbleSortPanel.reset();
jButton1.setEnabled(true);
}
});
}
class BubbleSortPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int searchSize = 20;
private int stepPosition = -1;
private boolean needNextPass = true;
private boolean isCylce = false;
private int iCycle = -1;
ArrayList<Integer> numbers = new ArrayList<>();
public BubbleSortPanel() {
for (int i = 1; i <= searchSize; i++) {
numbers.add(i);
}
reset();
}
public void nextStep() {
if(stepPosition == -1) {
stepPosition = 1;
} else {
if(!isCylce) {
stepPosition++;
}
}
if(stepPosition >= searchSize) {
jButton1.setEnabled(false);
stepPosition = -1;
} else {
if(!isCylce) {
needNextPass = false;
isCylce = true;
iCycle = 0;
} else if (isCylce) {
if (numbers.get(iCycle) > numbers.get(iCycle + 1)) {
// Swap list[i] with list[i + 1]
int temp = numbers.get(iCycle);
numbers.set(iCycle, numbers.get(iCycle + 1));
numbers.set(iCycle + 1, temp);
needNextPass = true; // Next pass still needed
}
iCycle++;
if(!(iCycle < numbers.size() - stepPosition)) {
isCylce = false;
//iCycle = 0;
}
}
if(!isCylce) {
if(!needNextPass) {
jButton1.setEnabled(false);
stepPosition = -1;
iCycle = -1;
}
}
}
repaint();
}
public void reset() {
Collections.shuffle(numbers);
stepPosition = -1;
needNextPass = true;
isCylce = false;
iCycle = -1;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int numberWidth = getWidth() / (searchSize + 2);
int numberHeight = getHeight() / (searchSize + 2);
for (int i = 0; i < searchSize; i++) {
g.drawRect(numberWidth * (i + 1), getHeight() - numberHeight * numbers.get(i), numberWidth, numberHeight * numbers.get(i));
g.drawString(numbers.get(i) + "", numberWidth * (i + 1), getHeight() - numberHeight * numbers.get(i) - (int)(numberHeight * 0.5));
}
if((iCycle != -1)&&(iCycle < searchSize)) {
g.fillRect(numberWidth * (iCycle + 1), getHeight() - numberHeight * numbers.get(iCycle), numberWidth, numberHeight * numbers.get(iCycle));
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Exercise16");
Exercise16 applet = new Exercise16();
frame.add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(960, 500);
frame.setMinimumSize(new Dimension(frame.getWidth(), frame.getHeight()));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Exercise17.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Exercise17 extends JApplet {
private static final long serialVersionUID = 1L;
private RadixSortPanel radixSortPanel = new RadixSortPanel();
private JButton jButton1 = new JButton("Step");
public Exercise17() {
setLayout(new BorderLayout());
add(radixSortPanel, BorderLayout.CENTER);
JPanel panel1 = new JPanel();
panel1.add(jButton1);
JButton jButton2 = new JButton("Reset");
panel1.add(jButton2);
add(panel1, BorderLayout.SOUTH);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
radixSortPanel.nextStep();
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
radixSortPanel.reset();
jButton1.setEnabled(true);
}
});
}
class RadixSortPanel extends JPanel {
private static final long serialVersionUID = 1L;
private int searchSize = 30;
private int maxOrder = 1000;
private int order = 1;
private boolean isCycle = false;
private int iCycle = 0;
private ArrayList<Integer>[] bucket;
int lastNumberCol = 0;
ArrayList<Integer> numbers = new ArrayList<>();
public RadixSortPanel() {
reset();
}
@SuppressWarnings("unchecked")
public void nextStep() {
if(!isCycle) {
bucket = new ArrayList[10];
for (int i = 0; i < bucket.length; i++) {
bucket[i] = new java.util.ArrayList<>();
}
isCycle = true;
iCycle = 0;
}
if(isCycle) {
bucket[(numbers.get(iCycle) / order) % 10].add(numbers.get(iCycle));
lastNumberCol = (numbers.get(iCycle) / order) % 10;
iCycle++;
if(iCycle >= numbers.size()) {
isCycle = false;
}
}
if(!isCycle) {
int k = 0;
for (int i = 0; i < bucket.length; i++) {
if (bucket[i] != null) {
for (int j = 0; j < bucket[i].size(); j++)
numbers.set(k++, bucket[i].get(j));
}
}
order *= 10;
if(order >= maxOrder) {
jButton1.setEnabled(false);
order = 1;
}
bucket = null;
}
repaint();
}
public void reset() {
numbers.clear();
for (int i = 1; i <= searchSize; i++) {
numbers.add((int)(Math.random() * maxOrder));
}
order = 1;
isCycle = false;
iCycle = 0;
bucket = null;
lastNumberCol = 0;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int numberWidth = getWidth() / (searchSize + 2);
int numberHeight = 20;
for (int i = 0; i < searchSize; i++) {
g.drawRect(numberWidth * (i + 1), 20, numberWidth, numberHeight);
g.drawString(numbers.get(i) + "", numberWidth * (i + 1) + 5, 35);
}
int columns = 10;
int columnWidth = getWidth() / columns;
int columnHeight = getHeight() - 80;
for (int i = 0; i < columns; i++) {
g.drawRect(columnWidth * i + 10, 60, columnWidth - 20, columnHeight);
if(bucket != null) {
for (int j = 0; j < bucket[i].size(); j++) {
if((i == lastNumberCol)&&(j == bucket[i].size() - 1)) {
g.setColor(Color.RED);
}
g.drawString(bucket[i].get(j) + "", columnWidth * i + 20, 90 + j * 20);
if((i == lastNumberCol)&&(j == bucket[i].size() - 1)) {
g.setColor(Color.BLACK);
}
}
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Exercise17");
Exercise17 applet = new Exercise17();
frame.add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(960, 500);
frame.setMinimumSize(new Dimension(frame.getWidth(), frame.getHeight()));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.