Java实现排序的可视化

内部排序

  • ->插入类排序
    • –>直接插入排序(insert sort)
    • –>折半插入排序(binary sort)
    • –>希尔排序(shell sort)
  • ->交换类排序
    • –>冒泡排序(bubble sort)
    • –>快速排序(quick sort)
  • ->选择类排序
    • –>简单选择排序(select sort)
    • –>树形选择排序
    • –>堆排序
  • ->归并排序
  • ->分配类排序
    • –>多关键字排序
    • –>链式基数排序

先看以下运行效果

github源码: github源码
运行视频:运行效果视频.
在这里插入图片描述

一、 类图

在这里插入图片描述

二、类列表

在这里插入图片描述

三、源代码
Sort接口

为各个排序方法类提供一个共同的方法

/**
 * 排序算法的接口
 * a interface of sorting
 * */
public interface Sort {	
	//进行排序(sorting)
	abstract void startSort();
}
Visual 抽象类

public abstract class Visual extends JPanel implements Sort,Runnable{
	//窗口
	public abstract void windowFrom();
}
VisibleSort
/**
 * 界面和数组的可视化
 * to make sort visually
 * */
public class VisibleSort extends Visual{
	JFrame jFrame;
	ArrayNeedToSort array;
	
	VisibleSort() {
		array = new ArrayNeedToSort();
		array.initalArray();
		array.messArray();
		windowFrom();
	}
	
	//排序可视化
	public void paint(Graphics g) {
		//每次画之前都要先清空画板
		g.clearRect(0, 0, 1500,800);
		

		
		for (int i = 0; i < 100; i++) {
			g.setColor(Color.BLACK);
			g.fillRect(i*10+100,(500-(array.get(i)*4)),10, array.get(i)*4);
		}
		
	}
	
	
	
	//窗口设置
	public void windowFrom() {
		jFrame = new JFrame();
		
		jFrame.setSize(1200,580);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setLocationRelativeTo(null);
		
		
		jFrame.add(this);
		jFrame.setVisible(true);
	}

	@Override
	public void startSort() {
		// TODO Auto-generated method stub		
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
}

MainTest类
/**
 * 主函数
 * the entrance of this program
 * /
public class MainTest {
	public static void main(String[] args) {
		//直接插入排序(insert sort)
		InsertSort insertSort = new InsertSort();
		insertSort.startSort();
		
		//冒泡排序(bubble sort)
		BubbleSort bubbleSort = new BubbleSort();
		bubbleSort.startSort();
		
		//希尔排序(shell sort)
		ShellSort shellSort = new ShellSort();
		shellSort.startSort();
		
		//快速排序(quick sort)
		QuickSort quickSort = new QuickSort();
		quickSort.startSort();
		
		//折半插入排序(binary sort)
		BinarySort binarySort = new BinarySort();
		binarySort.startSort();
	}
}
ArrayNeedToSort
/**
 * 用来排序的数组
 * the arrary ready to sort
 * */
public class ArrayNeedToSort {
	int[] array;
	
	ArrayNeedToSort() {
		array = new int[100];
	}
	
	//初始化数组
	public void initalArray() {
		for (int i = 0; i < 100; i++) {
			array[i] = i + 1;
		}
	}
	//伪随机打乱数组
	public void messArray() {
		for (int i = 0; i < 100; i++) {
			int temp = array[i];
			int j = (int)(Math.random()*100);
			if (i != j) {
				array[i] = array[j];
				array[j] = temp;
			}
		}
	}	
	//获得指定下标的数组元素
	public int get(int i) {
		if (i > 100 || i < 0) {
			System.err.println("下标错误!");
		}else {
			return array[i];
		}		
		return 0;
	}
	//设置指定下标的数组元素
	//即:temp替换  array[i]的值
	public void set(int temp,int i) {
		if (i > 100 || i < 0) {
			System.err.println("下标错误!");
		}else {
			array[i] = temp; //修改下标为i的数组的值
		}		
	}	
}
InsertSort类(直接插入排序)
/**
 * 插入排序----直接插入排序(insert sort---InsertSort)
 * */
public class InsertSort extends VisibleSort{	
	
	Thread thread ;
	
	InsertSort(){
		thread = new Thread(this);
		thread.start();
	}
	
	
	@Override
	public void run() {
		while (true) {
			try {
				startSort();
			} catch (Exception e) {
				System.err.println("错误!");
			}			
		}	
	}
	
	//直接插入排序可视化
	@Override
	public void startSort() {
		for (int i = 1; i < 100; i++) {
			int temp = super.array.get(i); //把要插入排序的数先暂存到临时变量中
			int j = i - 1; //用于标记插入位置
			while(temp < array.get(j)) {//寻找插入位置
				array.set(array.get(j), j+1);
				j -= 1;
				try {
					Thread.sleep(10); //每次重画都要有一定的延迟,为了看清!
				} catch (Exception e) {}
				super.repaint();				
			}
			array.set(temp, j+1);	
		}
		//排序完成停2s退出
		try {
			Thread.sleep(2000); 
		} catch (Exception e) {}
		super.jFrame.setVisible(false);
	}
}
BubbleSort类(冒泡排序)
/**
 * 交换类排序---冒泡排序(exchange sort---BubbleSort)
 * */
public class BubbleSort extends VisibleSort{
	Thread thread;
	
	BubbleSort(){
		thread = new Thread(this);
		thread.start();
	}
	
	
	@Override
	public void run() {
		while(true) {
			try {
				startSort();
			} catch (Exception e) {}
		}
		
	}

	@Override
	public void startSort() {
		try {
			Thread.sleep(500);
		} catch (Exception e) {}
		for (int i = 0; i <= 98; i++) {
			for (int j = 0; j <= 98-i; j++) {
				if (array.get(j) > array.get(j+1)) {
					//调换位置(change location)
					int temp = array.get(j);
					array.set(array.get(j+1), j); 
					array.set(temp, j+1);  
					//重画--redraw
					try {
						Thread.sleep(10);
					} catch (Exception e) {}
					repaint();
				}
			}
		}
		//悬停2s后隐藏 hide this frame after 2s
		try {
			Thread.sleep(2000);
		} catch (Exception e) {}
		super.jFrame.setVisible(false);
	}

}
ShellSort类(希尔排序)
/**
 * 插入排序---希尔排序(insert sort---shell sort)
 * */
public class ShellSort extends VisibleSort {

	Thread thread;
	int[] delta = {4,2,1}; //增量
	
	ShellSort(){
		thread = new Thread(this);
		thread.start();
	}
	
	@Override
	public void run() {
		while(true) {
			try {
				startSort();
			} catch (Exception e) {}
		}
		
	}

	@Override
	public void startSort() {
		try {
			Thread.sleep(500);
		} catch (Exception e) {}
		
		for (int i = 0; i < 3; i++) {
			shellSort(delta[i]);
		}
		
		try {
			Thread.sleep(2000);
		} catch (Exception e) {}
		super.jFrame.setVisible(false);
	}
	
	
	public void shellSort(int delta) {
		for (int i = delta; i < 100; i++) {
			if (array.get(i) < array.get(i-delta)) {
				int temp = array.get(i);
				//子序列内排序--subsequent sort
				int j;
				for (j = i-delta; j>=0 && temp < array.get(j); j-=delta) {
					array.set(array.get(j), j+delta);
					//重画--redraw
					try {
						Thread.sleep(10);
					} catch (Exception e) {}
					repaint();
				}
				array.set(temp, j+delta);	
			}
		}
	}

}
QuickSort类(快速排序)
/**
 * 交换类排序--快速排序(exchange sort--quick sort)
 * */
public class QuickSort extends VisibleSort{

	Thread thread;
	
	QuickSort(){
		thread = new Thread(this);
		thread.start();
	}
	
	
	@Override
	public void run() {
		try {
			startSort();
		} catch (Exception e) {}
	}

	@Override
	public void startSort() {
		int low = 0;
		int high = 99;
		try {
			Thread.sleep(1000);
		} catch (Exception e) {}
		QuickSort(low,high);	
		//2s后隐藏
		try {
			Thread.sleep(3000);
		} catch (Exception e) {}
		super.jFrame.setVisible(false);
	}
	public void QuickSort(int low ,int high) {
		if (low < high) {
			int pos = QKSort(low, high);
			QuickSort(low,pos-1);  //对左部子表排序
			QuickSort(pos+1,high); //对右部子表排序
		}
		
	}
	public int QKSort(int low,int high) {
		int temp = array.get(low); //基准记录
		while(low < high) {
			while(low < high && array.get(high) >= temp) {
				high--; //high从右向左找小于temp的数
			}
			if (low < high) { array.set(array.get(high), low); low++;}//找到放入左边“空位置”,此时右边有空位置
			while(low < high && array.get(low) <= temp) {
				low++; //low从左向右找大于temp的数
			}
			if(low < high) {array.set(array.get(low), high);high--;}//找到放入右边
			//重画--redraw
			try {
				Thread.sleep(10);
			} catch (Exception e) {}
			repaint();
		}
		array.set(temp, low);//最后中间 low==high为空位置		
		return low;	//返回基准记录的位置
	}

}
BinarySort类(折半插入排序)
/**
 * 插入排序---折半插入排序(insert sort---BinarySort)
 * */
public class BinarySort extends VisibleSort{

	Thread thread;
	
	BinarySort(){
		thread = new Thread(this);
		thread.start();
	}
		
	
	@Override
	public void run() {
		while(true) {
			try {
				startSort();
			} catch (Exception e) {
				System.err.println("错误!");
			}
		}		
	}
	@Override
	public void startSort() {
		try {
			Thread.sleep(500);
		} catch (Exception e) {}
		for (int i = 1; i < 100; i++) {
			int temp = super.array.get(i);
			int low = 0;
			int high = i - 1;
			while(low <= high) { //确定插入位置
				int mid = (low + high)/2;
				if (temp < array.get(mid)) {
					high = mid - 1;
				}else {
					low = mid + 1;
				}
			}
			//移动,插入
			for (int j = i-1; j >= low; j--) {
				array.set(array.get(j), j+1);
				try {
					Thread.sleep(10);
				} catch (Exception e) {}
				repaint();
			}
			array.set(temp, low);
		}
	}
}
SelectSort(简单选择排序)
public class SelectSort extends VisibleSort{

	Thread thread;
	SelectSort(){
		thread = new Thread(this);
		thread.start();
	}
	
	
	@Override
	public void run() {
		while(true) {
			try {
				startSort();
			} catch (Exception e) {}
			
		}
	}

	@Override
	public void startSort() {
		int k;
		//每次找一个最小的放到前面
		for (int i = 0; i < 100; i++) {
			k = i;
			//找最小的数
			for (int j = i+1; j < 100; j++) {
				if (array.get(j) < array.get(k)) {
					k = j;
				}
			}
			//交换位置
			if (k != i) {
				int temp = array.get(i);
				array.set(array.get(k), i);
				array.set(temp, k);
			}		
			//重画--redraw
			try {
				Thread.sleep(10);
			} catch (Exception e) {}
			repaint();
		}
		//悬停2s隐藏---hiding after 2s
		try {
			Thread.sleep(2000);
		} catch (Exception e) {}
		super.jFrame.setVisible(false);
	}

}
  • 20
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值