Monday, 23 January 2017

Chapter 23 Exercise 15, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

23.15 (Selection sort animation) Write a program that animates the selection sort algorithm. Create an array that consists of 20 distinct numbers from 1 to 20 in a random order. The array elements are displayed in a histogram, as shown in
Figure 23.20a. Clicking the Step button causes the program to perform an itera-
tion of the outer loop in the algorithm and repaints the histogram for the new
array. Color the last bar in the sorted subarray. 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. (You can easily modify the program to animate the insertion algorithm.)


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 Exercise15 extends JApplet {

 private static final long serialVersionUID = 1L;
 private SelectionSortPanel SelectionSortPanel = new SelectionSortPanel();
 private JButton jButton1 = new JButton("Step");
 
 public Exercise15() {
  setLayout(new BorderLayout());
  add(SelectionSortPanel, 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) {
    SelectionSortPanel.nextStep();
   }
  });
  
  jButton2.addActionListener(new ActionListener() {   
   @Override
   public void actionPerformed(ActionEvent e) {
    SelectionSortPanel.reset();
    jButton1.setEnabled(true);
   }
  });
 }
 
 class SelectionSortPanel extends JPanel {
  private static final long serialVersionUID = 1L;
  private int searchSize = 50;
  private int stepPosition = -1;
  ArrayList<Integer> numbers = new ArrayList<>();
  
  public SelectionSortPanel() {
   for (int i = 1; i <= searchSize; i++) {
    numbers.add(i);
   }
   reset();
  }
  
  public void nextStep() {
   stepPosition++;
   if(stepPosition >= searchSize) {
    jButton1.setEnabled(false);
    stepPosition = -1;
   } else {
    int currentMin = numbers.get(stepPosition);
    int currentMinIndex = stepPosition;

    for (int j = stepPosition + 1; j < numbers.size(); j++) {
     if (currentMin > numbers.get(j)) {
      currentMin = numbers.get(j);
      currentMinIndex = j;
     }
    }
    if (currentMinIndex != stepPosition) {
     numbers.set(currentMinIndex, numbers.get(stepPosition));
     numbers.set(stepPosition, currentMin);
    }    
   }
   repaint();  
  }
  
  public void reset() {
   Collections.shuffle(numbers);
   stepPosition = -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((stepPosition != -1)&&(stepPosition < searchSize)) {
    g.fillRect(numberWidth * (stepPosition + 1), getHeight() - numberHeight * numbers.get(stepPosition), numberWidth, numberHeight * numbers.get(stepPosition));
   }
  }

 }

 public static void main(String[] args) {
  JFrame frame = new JFrame("Exercise15");
  Exercise15 applet = new Exercise15();
  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);
 }
}

No comments :

Post a Comment