利用JFram实现经典排序算法动画

package demo.test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * 图形方式显示经典排序
 * @author WZG
 *
 */
public class SortX extends JFrame {
	private JLabel[] n;
	private JButton start;
	private JButton start1;
	private JButton start2;
	private JButton start3;
	private JButton start4;
	private JButton start5;
	private JTextField in;

	public SortX2() {
		this.getContentPane().setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(488, 200);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setAlwaysOnTop(true);
		start = new JButton("快速排序");
		start1 = new JButton("冒泡排序");
		start2 = new JButton("选择排序");
		start3 = new JButton("插入排序");
		start4 = new JButton("归并排序");
		start5 = new JButton("堆   排序");
		JLabel l = new JLabel("输入10组内数字:");
		l.setBounds(10, 18, 120, 20);
		add(l);
		in = new JTextField("10,212,7,456,33,2,55,6,50,97");
		in.setBounds(118, 18, 256, 20);
		add(in);
		start.setBounds(380, 18, 88, 20);
		start1.setBounds(16, 40, 86, 18);
		start2.setBounds(104, 40, 86, 18);
		start3.setBounds(192, 40, 86, 18);
		start4.setBounds(278, 40, 86, 18);
		start5.setBounds(366, 40, 86, 18);
		add(start);
		add(start1);
		add(start2);
		add(start3);
		add(start4);
		add(start5);
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sort(0);
			}
		});
		start1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sort(1);
			}
		});
		start2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sort(2);
			}
		});
		start3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sort(3);
			}
		});
		start4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sort(4);
			}
		});
		start5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sort(5);
			}
		});
		n = new JLabel[10];
		for (int i = 0; i < n.length; i++) {
			n[i] = new JLabel();
			n[i].setFont(new Font("", Font.BOLD, 14));
			n[i].setForeground(Color.gray);
			n[i].setHorizontalAlignment(JLabel.CENTER);
			add(n[i]);
		}
	}
	
	private void sort(int x) {
		setTitle("排序演示");
		try {
			String src = in.getText();
			src = src.replaceAll("[^0-9,]", "");
			String[] sp = src.split(",");
			final int[] v = new int[sp.length];
			int left = 0, w = 40, offx = 0, offy = 95;
			for (int i = 0; i < n.length; i++)
				n[i].setText(null);
			for (int i = 0; i < sp.length; i++) {
				n[i].setText(sp[i]);
				v[i] = Integer.valueOf(sp[i]);
				n[i].setSize(w, 20);
				n[i].setLocation((left += w) + offx, offy);
			}
			new Thread() {
				public void run() {
					//快速排序
					if(x==0) {
						start.setEnabled(false);
						s(500);
						try {
							for (int i = 0; i < n.length - 1; i++) {
								if (v[i] > v[i + 1]) {
									JLabel l = n[i];
									n[i] = n[i + 1];
									n[i + 1] = l;
									n[i].setForeground(Color.blue);
									for (int k = 0; k < 7; k++) {//闪烁数字
										n[i].setVisible(k % 2 == 0);
										s(123);
									}
									swap(n[i], n[i + 1]);
									n[i].setForeground(Color.gray);
									v[i] += v[i + 1];
									v[i + 1] = v[i] - v[i + 1];
									v[i] -= v[i + 1];
									i -= i == 0 ? 1 : 2;
								}
							}
						} catch (Exception e) {
						}
						start.setEnabled(true);
					}else if(x==1) {	//冒泡排序
						start1.setEnabled(false);
						s(500);
						try {
							for (int j = 0; j < n.length - 1; j++) {
								for(int i = 0; i<n.length-1-j;i++) {
									if(v[i] > v[i + 1]) {
										JLabel l = n[i+1];
										n[i+1] = n[i];
										n[i] = l;
										n[i].setForeground(Color.blue);
										for (int k = 0; k < 7; k++) {//闪烁数字
											n[i].setVisible(k % 2 == 0);
											s(123);
										}
										swap(n[i], n[i + 1]);
										n[i].setForeground(Color.gray);
										int temp =v[i+1];
										v[i+1]=v[i];
										v[i]=temp;
									}
								}
							}
						} catch (Exception e) {
						}
						start1.setEnabled(true);
					}else if(x==2) {	//选择排序
						start2.setEnabled(false);
						s(500);
						try {
							int minIndex;
							for (int i = 0; i < n.length - 1; i++) {
								minIndex = i;
								for (int j = i+1; j < n.length; j++) {
									if(v[j] < v[minIndex]) {
										minIndex = j;
									}
								}
								JLabel l = n[i];
								n[i] = n[minIndex];
								n[minIndex] = l;
								n[i].setForeground(Color.blue);
								for (int k = 0; k < 7; k++) {//闪烁数字
									n[minIndex].setVisible(k % 2 == 0);
									s(123);
								}
								swap(n[i], n[minIndex]);
								n[i].setForeground(Color.gray);
								int temp =v[i];
								v[i]=v[minIndex];
								v[minIndex]=temp;
							}
						} catch (Exception e) {
						}
						start2.setEnabled(true);
					
					}else if(x==3) {	//插入排序
						start3.setEnabled(false);
						s(500);
						try {
							int preIndex, current;
							for (int i = 1; i < n.length; i++) {
								preIndex = i - 1;
								current = v[i];
								while(preIndex >= 0 && v[preIndex] > current) {
						            v[preIndex + 1] = v[preIndex];
						            
						            JLabel l = n[preIndex + 1];
									n[preIndex + 1] = n[preIndex];
									n[preIndex] = l;
									n[preIndex].setForeground(Color.blue);
									for (int k = 0; k < 7; k++) {//闪烁数字
										n[preIndex].setVisible(k % 2 == 0);
										s(123);
									}
									swap(n[preIndex], n[preIndex+1]);
									n[preIndex].setForeground(Color.gray);
						            preIndex--;
						        }
						        v[preIndex + 1] = current;
							}
						} catch (Exception e) {
						}
						start3.setEnabled(true);
					}else if(x==4) {	//归并排序
						start4.setEnabled(false);
						s(500);
						try {
							//编码域
							
						} catch (Exception e) {
						}
						start4.setEnabled(true);
					
					}else if (x==5) {	//堆排序
						start5.setEnabled(false);
						s(500);
						try {
							//编码域
							
						} catch (Exception e) {
						}
						start5.setEnabled(true);
					}
				}
				private void swap(JLabel a, JLabel b) {
					JLabel t = a;
					a = b;
					b = t;
					Point pa = a.getLocation();
					Point pb = b.getLocation();
					int x1, x2, y1, y2;
					x1 = pa.x;
					x2 = pb.x;
					y1 = pa.y;
					y2 = pb.y;
					int delay = 10;
					while (x1 < (x1 + x2) / 2) {	
						a.setLocation(++x1, y1++);
						b.setLocation(--x2, y2--);
						s(delay);
					}
					while (x1 < pb.x) {
						a.setLocation(++x1, y1--);	//a标签向下,b标签向上跳换位置
						b.setLocation(--x2, y2++);
						s(delay);
					}
					a.setLocation(pb);
					b.setLocation(pa);
				}

				private void s(int i) {
					try {
						sleep(i);
					} catch (Exception e) {
					}
				}
			}.start();
		} catch (Exception e) {
			e.printStackTrace();
			setTitle("请检查输入的数据,只能输入10组哦");
		}
	}

	public static void main(String[] args) {
		new SortX2().setVisible(true);
	}
}

实现了除了归并排序、堆排序的动画

后面两个排序没有实现 有好的想法可以在下面留言

 如果觉得此文章对你有用,请关注下面公众号,博主会不定期分享技术文章,一起成长,共同进步!:)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值