📄 sortingdriver.java
字号:
package sortingAnimation;
import algorithmImp.*;
/**
* 排序算法的驱动程序,其主要作用是开启一个线程,运行待演示的排序算法。
*
* @author 周晓聪
* @since 2007/6/1
* @version 1.0
*
*/
public class SortingDriver {
// 定义一些枚举值用于选择某个排序算法
public final static int BUBBLE_SORT = 0; // 冒泡排序
public final static int SELECT_SORT = 1; // 选择排序
public final static int INSERT_SORT = 2; // 插入排序
public final static int QUICK_SORT = 3; // 快速排序
public final static int MERGE_SORT = 4; // 堆排序
public final static int HEAP_SORT = 5; // 堆排序
public final static int SHELL_SORT = 6; // 希尔排序
public final static int COUNT_SORT = 10; // 计数排序
public final static int RADIX_SORT = 11; // 基数排序
public final static int EXIT_SORT = -1;
private final int MAX_SIZE = 1000; // 用于排序的数组最大长度
private final int MAX_VALUE = 499;
private int[] data; // 用于排序的数组
private SortAlgorithmImp sortor; // 排序算法实现者
int type; // 所选择的排序算法,其值目标取 BUBBLE_SORT 等枚举值之一
int size = MAX_SIZE;
int maxValue = MAX_VALUE;
/**
* 选择的排序算法
* @param t 取BUBBLE_SORT 表示冒泡排序,SELECT_SORT 表示选择排序,INSERT_SORT 表示插入排序
*/
public SortingDriver(int t, int dataSize, int maxValue) {
type = t;
size = Math.min(MAX_SIZE, dataSize);
data = new int[size];
this.maxValue = maxValue;
}
/**
* 初始化待排序数组,开启线程,运行排序算法
*/
public void start() {
initialize();
Thread thread = new Thread(new ExecutionThread());
thread.start();
}
/**
* 使用随机数初始化待排序数组
*
*/
public void initialize() {
for (int i = 0; i < size; i++) {
data[i] = 1 + (int)(Math.random() * maxValue);
}
}
/**
*
* 这个内部类的作用是实现作为线程必需的接口 run() ,在该函数中启动排序算法
*
*/
private class ExecutionThread implements Runnable {
public void run() {
if (type == BUBBLE_SORT) sortor = new BubbleSortImp();
else if (type == INSERT_SORT) sortor = new InsertSortImp();
else if (type == SELECT_SORT) sortor = new SelectSortImp();
else if (type == QUICK_SORT) sortor = new QuickSortImp();
else if (type == MERGE_SORT) sortor = new MergeSortImp();
else if (type == HEAP_SORT) sortor = new HeapSortImp();
else if (type == SHELL_SORT) sortor = new ShellSortImp();
else sortor = new InsertSortImp();
sortor.initialize(data);
sortor.sorting();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -