冒泡排序动态演示java
时间: 2023-11-09 13:01:51 浏览: 45
以下是冒泡排序的Java代码和动态演示:
```java
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
动态演示可以使用Java Swing或JavaFX等GUI库实现,具体实现方式可以参考以下链接:
https://www.geeksforgeeks.org/bubble-sort-visualization-using-java/
相关问题
java swing 排序动态演示代码
以下是使用Java Swing实现排序算法动态演示的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class SortingAlgorithmDemo extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton generateButton, bubbleSortButton, selectionSortButton, insertionSortButton;
private JTextField arraySizeField;
private int[] arr;
public SortingAlgorithmDemo() {
setTitle("Sorting Algorithm Demo");
setBounds(100, 100, 800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel arraySizeLabel = new JLabel("Array Size:");
arraySizeLabel.setBounds(20, 20, 100, 30);
arraySizeField = new JTextField();
arraySizeField.setBounds(120, 20, 100, 30);
generateButton = new JButton("Generate");
generateButton.setBounds(250, 20, 100, 30);
generateButton.addActionListener(this);
bubbleSortButton = new JButton("Bubble Sort");
bubbleSortButton.setBounds(400, 20, 100, 30);
bubbleSortButton.addActionListener(this);
selectionSortButton = new JButton("Selection Sort");
selectionSortButton.setBounds(550, 20, 100, 30);
selectionSortButton.addActionListener(this);
insertionSortButton = new JButton("Insertion Sort");
insertionSortButton.setBounds(700, 20, 100, 30);
insertionSortButton.addActionListener(this);
panel = new JPanel();
panel.setLayout(null);
panel.add(arraySizeLabel);
panel.add(arraySizeField);
panel.add(generateButton);
panel.add(bubbleSortButton);
panel.add(selectionSortButton);
panel.add(insertionSortButton);
setContentPane(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == generateButton) {
int size = Integer.parseInt(arraySizeField.getText());
arr = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) {
arr[i] = random.nextInt(100);
}
repaint();
} else if (e.getSource() == bubbleSortButton) {
bubbleSort(arr);
} else if (e.getSource() == selectionSortButton) {
selectionSort(arr);
} else if (e.getSource() == insertionSortButton) {
insertionSort(arr);
}
}
private void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (arr != null) {
int n = arr.length;
int width = getWidth() / n;
int height = getHeight() - 100;
g.setColor(Color.BLUE);
for (int i = 0; i < n; i++) {
g.fillRect(i * width, height - arr[i] * 3, width, arr[i] * 3);
}
}
}
public static void main(String[] args) {
new SortingAlgorithmDemo();
}
}
```
这个程序演示了三种常见的排序算法:冒泡排序、选择排序和插入排序。用户可以指定数组大小并生成随机数组,然后选择一个排序算法演示排序过程。程序使用Java Swing绘制直方图来显示数组元素。通过每次交换或移动数组元素时重绘直方图,用户可以实时地看到排序算法的执行过程。
java swing 冒泡排序 实时 显示 演示
好的,我可以为您演示一个 Java Swing 冒泡排序实时显示的程序。以下是程序的代码:
```java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BubbleSortVisualizer extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton button;
private JTextField textField;
private int[] array = { 5, 3, 1, 2, 4 };
private int delay = 500;
public BubbleSortVisualizer() {
setTitle("Bubble Sort Visualizer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
setResizable(false);
panel = new JPanel();
panel.setLayout(new BorderLayout());
add(panel);
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
panel.add(topPanel, BorderLayout.NORTH);
JLabel label = new JLabel("Array: ");
topPanel.add(label);
textField = new JTextField(20);
textField.setEditable(false);
topPanel.add(textField);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
panel.add(bottomPanel, BorderLayout.SOUTH);
button = new JButton("Sort");
bottomPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread thread = new Thread(new Runnable() {
public void run() {
bubbleSort();
}
});
thread.start();
}
});
setVisible(true);
}
private void bubbleSort() {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
updateTextField();
sleep();
}
}
}
}
private void updateTextField() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.length; i++) {
stringBuilder.append(array[i]);
if (i != array.length - 1) {
stringBuilder.append(", ");
}
}
textField.setText(stringBuilder.toString());
}
private void sleep() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new BubbleSortVisualizer();
}
}
```
运行程序后,您可以在窗口中看到一个文本框和一个“Sort”按钮。文本框显示了初始数组的值(即 `5, 3, 1, 2, 4`)。当您点击“Sort”按钮时,程序将开始使用冒泡排序对数组进行排序,并在每次交换时更新文本框中的值,以实现实时演示。程序默认的延迟时间为 500 毫秒,您可以根据需要更改此值。