Java USES selection sort to sort the array implementation code

  • 2020-04-01 02:56:41
  • OfStack

Write a program to convert the input string into a one-dimensional array and sort the array using the selection sort method.

The idea is as follows:

Click "generate Random number" button to create Random Random number object;
Use JTextArea's setText() method to empty the text field;
Creates an integer one-dimensional array to allocate space of length 10.
Initialize array elements, use nextInt() method of Random class to generate Random Numbers within 50, and use append() method of JTextArea class to display array elements in text field control.
Click the "sort" button to clear the text field using the setText() method of the JTextArea class.
The double-layer for loop is used to sort every time from the second element to the last element. The elements involved in the sorting are traversed to find the index of the array corresponding to the maximum value.
Swap two Numbers at position array. Length - I and index(maximum value), so that the maximum value found after each sequence of sorting is at the end of the sequence involved in that sequence.
Use the for loop to iterate through the groups, and use the append method of the Random class to display the sorted array elements in the text field.
The code is as follows:


import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class SelectSort extends JFrame {

    
    private static final long serialVersionUID = 6824538613659403529L;
    private JPanel contentPane;

    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SelectSort frame = new SelectSort();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public SelectSort() {
        setTitle(" Sort an array using selection sort ");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 1.0, 0.0,
                Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPane.add(scrollPane, gbc_scrollPane);

        textArea1 = new JTextArea();
        scrollPane.setViewportView(textArea1);

        JButton button = new JButton(" Generate random number ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        GridBagConstraints gbc_button = new GridBagConstraints();
        gbc_button.insets = new Insets(0, 0, 5, 0);
        gbc_button.gridx = 0;
        gbc_button.gridy = 1;
        contentPane.add(button, gbc_button);

        JScrollPane scrollPane_1 = new JScrollPane();
        GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
        gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
        gbc_scrollPane_1.gridx = 0;
        gbc_scrollPane_1.gridy = 2;
        contentPane.add(scrollPane_1, gbc_scrollPane_1);

        textArea2 = new JTextArea();
        scrollPane_1.setViewportView(textArea2);

        JButton button_1 = new JButton(" The sorting ");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_1_actionPerformed(e);
            }
        });
        GridBagConstraints gbc_button_1 = new GridBagConstraints();
        gbc_button_1.gridx = 0;
        gbc_button_1.gridy = 3;
        contentPane.add(button_1, gbc_button_1);
    }

    private int[] array = new int[10];
    private JTextArea textArea1;
    private JTextArea textArea2;

    protected void do_button_actionPerformed(ActionEvent e) {
        Random random = new Random();//Create a random number object
        textArea1.setText("");//Empty text field
        for (int i = 0; i < array.length; i++) {//Initializes the array element
            array[i] = random.nextInt(50);//Generate random Numbers up to 50
            textArea1.append(array[i]+"  ");//Displays the array elements in the text field control
        }
    }

    protected void do_button_1_actionPerformed(ActionEvent e) {
        textArea2.setText("");//Empty text field
        int index;
        for (int i = 1; i < array.length; i++) {
            index = 0;
            for (int j = 1; j <= array.length - i; j++) {
                if (array[j] > array[index]) {
                    index = j;//Find the maximum
                }
            }
            //Swap two Numbers at the position array. Length - I and index(maximum)
            int temp = array[array.length - i];
            array[array.length - i] = array[index];
            array[index] = temp;
        }
        for (int i = 0; i < array.length; i++) {
            textArea2.append(array[i] + "  ");//Displays the sorted array elements in the text field
        }
    }
}

The effect is as follows:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201402/2014218160457901.png" >


Related articles: