github源码: github源码
运行视频:运行效果视频.
为各个排序方法类提供一个共同的方法
/**
* 排序算法的接口
* a interface of sorting
* */
public interface Sort {
//进行排序(sorting)
abstract void startSort();
}
public abstract class Visual extends JPanel implements Sort,Runnable{
//窗口
public abstract void windowFrom();
}
/**
* 界面和数组的可视化
* 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
}
}
/**
* 主函数
* 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();
}
}
/**
* 用来排序的数组
* 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的数组的值
}
}
}
/**
* 插入排序----直接插入排序(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);
}
}
/**
* 交换类排序---冒泡排序(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);
}
}
/**
* 插入排序---希尔排序(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);
}
}
}
}
/**
* 交换类排序--快速排序(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; //返回基准记录的位置
}
}
/**
* 插入排序---折半插入排序(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);
}
}
}
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);
}
}