Java使用選擇排序法對數組排序實現代碼_java

來源:互聯網
上載者:User

編寫程式,實現將輸入的字串轉換為一維數組,並使用選擇排序法對數組進行排序。

思路如下:

點擊"產生隨機數"按鈕,建立Random隨機數對象;
使用JTextArea的setText()方法清空文本域;
建立一個整型一維數組,分配長度為10的空間;
初始化數組元素,使用Random類的nextInt()方法產生50以內的隨機數,使用JTextArea類的append()方法把數組元素顯示在文本域控制項中;
點擊"排序"按鈕,使用JTextArea類的setText()方法清空文本域;
使用雙層for迴圈,對從第二個元素到最後一個元素的每一趟排序,對該趟排序所涉及的元素進行遍曆,尋找最大值對應的數組下標;
交換在位置array.length-i和index(最大值)兩個數,使得每趟排序後找到的最大值都在該趟排序所涉及的數列的最後;
使用for迴圈遍曆數組,使用Random類的append方法把排序後的數組元素顯示到文本域中。
代碼如下:

複製代碼 代碼如下:

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;

    /**
     * Launch the application.
     */
    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();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SelectSort() {
        setTitle("使用選擇排序法對數組排序");
        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("產生隨機數");
        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("排序");
        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();// 建立隨機數對象
        textArea1.setText("");// 清空文本域
        for (int i = 0; i < array.length; i++) {// 初始化數組元素
            array[i] = random.nextInt(50);// 產生50以內的隨機數
            textArea1.append(array[i]+"  ");// 把數組元素顯示的文本域控制項中
        }
    }

    protected void do_button_1_actionPerformed(ActionEvent e) {
        textArea2.setText("");// 清空文本域
        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;// 尋找最大值
                }
            }
            // 交換在位置array.length-i和index(最大值)兩個數
            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] + "  ");// 把排序後的數組元素顯示到文本域中
        }
    }
}

效果如圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.