这是我在学数据结构时课程设计遇到的一道关于八大排序算法执行效率的小题,感觉挺有意思,将我自己完成的程序分享一下。
一、题目介绍:排序算法的实现
编程实现直接插入排序、希尔排序、改进的冒泡排序、快速排序、直接选择排序、堆排序、归并排序和链式基数排序。要求先输入10个整数,输出各种排序算法每一趟排序的结果;然后,随机产生3万个数,对其进行排序,观察其结果,并测试各排序算法的执行时间,比较执行效率。
二、总体思路
(1)描述待排序的顺序表类、顺序表记录关键字类、顺序表记录类
(2)根据所学内容回顾八种排序的基本思想,完成八种排序的代码
(3)将题目分为输入10个整数,输出各种排序算法每一趟排序的结果和随机产生3万个数比较八种排序的执行效率两部分。
(4)将两部分写成Test类和TimeTest类完成全部题目要求。
三、逻辑设计
函数或类的具体定义和功能
类 | 函数 | 函数说明 | |
SeqList | insertSort | 直接插入排序 | |
shellSort | 希尔排序 | ||
quickSort | 快速排序 | ||
selectSort | 直接选择排序 | ||
HeapSort | 堆排序 | ||
Mergesort | 归并排序 | ||
bubbleSort | 改进的冒泡排序 | ||
Radix | Max | 返回每个数的位数 | |
RadixEvery | 第n+1趟链式基数排序 | ||
RadixAll | 链式基数排序 | ||
traversal | 遍历输出序列 | ||
insert | 在i位置插入节点s,头插法 | ||
getLength | 返回链表长度 |
代码模块划分
四、代码
public class KeyType implements Comparable<KeyType>{
public int key;
public KeyType(){}
public KeyType(int key){
this.key=key;
}
@Override
public String toString(){
return key+"";
}
@Override
public int compareTo(KeyType another){
int thisVal=this.key;
int anotherVal=another.key;
return (thisVal < anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
}
public class Node{
public int data;
public Node next;
public Node(){
this(0,null);
}
public Node (int data){
this(data,null);
}
public Node(int data,Node next){
this.data=data;
this.next=next;
}
}
public class Radix {
public Node[] list1 ;
public Node head;
public int max;
public Radix(int[] list2, int max) throws Exception {//list2包装成Node,传入int数组,挂到head上
this.max = max;
head = new Node();
int len = list2.length;//头插法创建链表,存入数据
while(--len>=0) {
Node s = new Node(list2[len]);
insert(head,0,s);
}
list1 = new Node[10];
for(int i=0;i<10;i++) {//十进制基数数组初始化
list1[i] = new Node(i);
}
}
public void RadixEvery(int n) throws Exception {//第n+1趟链式基数排序//第n+1趟即为对第n+1位数排序
Node p = head.next;
int key,ans;
while(p!=null) {//取第n+1位数排序
if(n==0)key = p.data%10;
else key = (p.data/(int)Math.pow(10,n)%10);
ans = 0;
while(ans<10) {//插入位置
if(key==list1[ans].data) {//挂链
Node node = new Node(p.data);
int w = getLength(list1[ans]);//求长度
insert(list1[ans],w-1,node);//头插
break;
}
else {
ans++;
}
}
p = p.next;
}
}
public void RadixAll() throws Exception {
//int m = Max(max);
for(int i=0;i<max;i++) {
for(int k = 0;k<10;k++) {//清空基数链表
list1[k].next = null;
}
RadixEvery(i);
head.next = null;//清空链表
for(int j=0;j<10;j++) {//收集
Node p = list1[j].next;
while(p!=null) {
int w = getLength(head);
Node q = p.next;
insert(head,w-1,p);
p = q;
}
}
System.out.print("第"+(i+1)+"趟的结果为:");
traversal();
System.out.println();
}
}
public void traversal() {//遍历输出
Node p = head.next;
int count = 0;
while(p!=null) {
if(count%50==0)System.out.println();
System.out.print(p.data+" ");
p = p.next;
count++;
}
}
public void insert(Node h,int i, Node s) throws Exception {//插入
Node p=h ;//初始化
int j=-1;//无头结点
while(p!=null && j<i-1){//找插入位置
p=p.next; j++; }
if(j>i-1 || p==null) //j>i-1,i为负数
throw new Exception("插入位置不合法");
s.next = p.next;
p.next = s;
}
public int getLength(Node h) {//链表长度
Node p = h;
int count = 0;
while(p!=null) {
p = p.next;
count++;
}
return count;
}
}
public class RadixForTime {
public Node[] list1 ;
public Node head;
public int max;
public RadixForTime(int[] list2, int max) throws Exception {
this.max = max;
head = new Node();
int len = list2.length;//头插法创建链表,存入数据
while(--len>=0) {
Node s = new Node(list2[len]);
insert(head,0,s);
}
list1 = new Node[10];
for(int i=0;i<10;i++) {//十进制基数数组初始化
list1[i] = new Node(i);
}
}
public int Max(int n) {
int count = 0;
while(n>0) {
count++;
n = n/10;
}
return count;
}
public void RadixEvery(int n) throws Exception {//第n+1趟链式基数排序
Node p = head.next;
int key = 0,ans = 0;
while(p!=null) {
if(n==0)key = p.data%10;//n=0时进行运算会报出错误
else key = (p.data/(int )Math.pow(10,n)%10);
ans = 0;
while(ans<10) {
if(key==list1[ans].data) {
Node node = new Node(p.data);
int w = getLength(list1[ans]);
insert(list1[ans],w-1,node);
break;
}
else {
ans++;
}
}
p = p.next;
}
}
public void RadixAll() throws Exception {
//int m = Max(max);
for(int i=0;i<max;i++) {
for(int k = 0;k<10;k++) {//清空基数链表
list1[k].next = null;
}
RadixEvery(i);
head.next = null;//清空链表
for(int j=0;j<10;j++) {
Node p = list1[j].next;
while(p!=null) {
int w = getLength(head);
Node q = p.next;
insert(head,w-1,p);
p = q;
}
}
}
}
public void traversal() {//遍历输出
Node p = head.next;
int count = 0;
while(p!=null) {
if(count%50==0)System.out.println();
System.out.print(p.data+" ");
p = p.next;
count++;
}
}
public void insert(Node h,int i, Node s) throws Exception {//插入
Node p=h ;//初始化
int j=-1;//无头结点
while(p!=null && j<i-1){//找插入位置
p=p.next; j++; }
if(j>i-1 || p==null) //j>i-1,i为负数
throw new Exception("插入位置不合法");
s.next = p.next;
p.next = s;
}
public int getLength(Node h) {//链表长度
Node p = h;
int count = 0;
while(p!=null) {
p = p.next;
count++;
}
return count;
}
}
public class RecordNode {
public Comparable key;
public Object element;
public RecordNode(Comparable key) {
this.key=key;
}
public RecordNode(Comparable key,Object element) {
this.key=key;
this.element=element;
}
}
public class SeqList {
public RecordNode[] r;
public int curlen;
//构造函数
public SeqList(int maxSize) {
this.r =new RecordNode[maxSize];
this.curlen=0;
}
//在排序表里边插入一个数据
public void insert(int i, RecordNode x)throws Exception{
if(curlen== r.length) {
throw new Exception("顺序表已满");
}
if(i<0||i>curlen) {
throw new Exception("插入位置不合理");
}
for(int j=curlen;j>i;j--) {
r[j]= r[j-1];
}
r[i]=x;
this.curlen++;
}
//输出排序表的长度
public int length() {
return curlen;
}
//简单的输出排序表里的内容
public void display(RecordNode[] renew){//用来输出新序列
for(int i=0;i< renew.length;i++){
System.out.print(renew[i].key+" ");
}
System.out.println();
}
public void display(){//用来输出原序列
for(int i=0;i< this.curlen;i++){
System.out.print(r[i].key+" ");
}
System.out.println();
}
//直接插入
public void insertSort() {
RecordNode[]reinsert=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reinsert[count]=r[count];
}
RecordNode temp;
int i, j,k=1;
for (i = 1; i < this.curlen; i++) { //n-1趟扫描
temp = reinsert[i]; //第i条记录暂存在temp中
for (j = i - 1; j >= 0 && temp.key.compareTo(reinsert[j].key) < 0; j--) {
reinsert[j + 1] = reinsert[j]; //后移
}
reinsert[j + 1] = temp;
System.out.print("第"+i+"趟");
display(reinsert);
}
}
//直接插入//用来测时间
public void insertSortfortime() {
RecordNode[]reinsert=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reinsert[count]=r[count];
}
RecordNode temp;
int i, j,k=1;
for (i = 1; i < this.curlen; i++) { //n-1趟扫描
temp = reinsert[i]; //第i条记录暂存在temp中
for (j = i - 1; j >= 0 && temp.key.compareTo(reinsert[j].key) < 0; j--) {
reinsert[j + 1] = reinsert[j]; //后移
}
reinsert[j + 1] = temp;
}
}
//希尔
public void shellSort() {
RecordNode[]reshell=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reshell[count]=r[count];
}
RecordNode temp;
int i, j,m=1;
int[] d ={5,3,1};
for (int k = 0; k < d.length; k++) {
int dk = d[k];
for (i = dk; i < this.curlen; i++) {
temp = reshell[i];
for (j = i - dk; j >= 0 && temp.key.compareTo(reshell[j].key) < 0; j -= dk) {
reshell[j + dk] = reshell[j];
}
reshell[j + dk] = temp;
}
System.out.print("第"+(k+1)+"趟");
display(reshell);
}
}
//希尔//用来测时间
public void shellSortfortime() {
RecordNode[]reshell=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reshell[count]=r[count];
}
RecordNode temp;
int i, j,m=1;
int[] d ={5,3,1};
for (int k = 0; k < d.length; k++) {
int dk = d[k];
for (i = dk; i < this.curlen; i++) {
temp = reshell[i];
for (j = i - dk; j >= 0 && temp.key.compareTo(reshell[j].key) < 0; j -= dk) {
reshell[j + dk] = reshell[j];
}
reshell[j + dk] = temp;
}
}
}
//改进的冒泡排序并输出每趟排序结果
public void bubbleSort() {
RecordNode[]rebubble=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
rebubble[count]=r[count];
}
RecordNode temp;
boolean flag = true; //是否交换的标记
for (int i = 1; i < this.curlen && flag; i++) {
//有交换时再进行下一趟,最多n-1趟
flag = false; //记录未交换标志
for (int j = 0; j < this.curlen - i; j++) {
if (rebubble[j].key.compareTo(rebubble[j + 1].key) > 0) {
//逆序时,交换
temp = rebubble[j];
rebubble[j] = rebubble[j + 1];
rebubble[j + 1] = temp;
flag = true; //记录交换标志
}
}
System.out.print("第"+i+"趟");
display(rebubble);
}
}
//改进的冒泡排序并输出每趟排序结果//用来测试时间
public void bubbleSortfortime() {
RecordNode[]rebubble=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
rebubble[count]=r[count];
}
RecordNode temp;
boolean flag = true; //是否交换的标记
for (int i = 1; i < this.curlen && flag; i++) {
//有交换时再进行下一趟,最多n-1趟
flag = false; //记录未交换标志
for (int j = 0; j < this.curlen - i; j++) {
if (rebubble[j].key.compareTo(rebubble[j + 1].key) > 0) {
//逆序时,交换
temp = rebubble[j];
rebubble[j] = rebubble[j + 1];
rebubble[j + 1] = temp;
flag = true; //记录交换标志
}
}
}
}
//快速排序
public static int countforpartition=0;
public int Partition (RecordNode[] repartition,int i, int j){
RecordNode pivot = repartition[i];
while (i<j) {
while(i<j&& pivot.key.compareTo(repartition[j].key)<=0) {
j--;
}
if (i<j) {
repartition[i] = repartition[j];
i++;
}
while (i<j && pivot.key.compareTo(repartition[i].key)>0) {
i++;
}
if (i<j) {
repartition[j] = repartition[i];
j--;
}
}
repartition[i] =pivot;// 将基准值放至中央位置完成一次循环
return i;
}
public void qSort(RecordNode[] repartition,int low,int high){
//int count=0;
if(low<high){
int pivotolc = Partition(repartition,low,high);
countforpartition++;
System.out.print("第"+countforpartition+"趟");
display(repartition);
qSort(repartition,low,pivotolc-1);// 将左边的无序数组重复上面的操作
qSort(repartition,pivotolc+1,high);// 将右边的无序数组重复上面的操作
}
}
public void quickSort(){
RecordNode[]repartition=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
repartition[count]=r[count];
}
qSort(repartition,0,this.curlen-1);
}
//快速排序测试时间
public int Partitionfortime (RecordNode[] repartition,int i, int j){
RecordNode pivot = repartition[i];
while (i<j) {
while(i<j&& pivot.key.compareTo(repartition[j].key)<=0) {
j--;
}
if (i<j) {
repartition[i] = repartition[j];
i++;
}
while (i<j && pivot.key.compareTo(repartition[i].key)>0) {
i++;
}
if (i<j) {
repartition[j] = repartition[i];
j--;
}
}
repartition[i] =pivot;// 将基准值放至中央位置完成一次循环
return i;
}
public void qSortfortime(RecordNode[] repartition,int low,int high){
//int count=0;
if(low<high){
int pivotolc = Partition(repartition,low,high);
qSortfortime(repartition,low,pivotolc-1);// 将左边的无序数组重复上面的操作
qSortfortime(repartition,pivotolc+1,high);// 将右边的无序数组重复上面的操作
}
}
public void quickSortfortime(){
RecordNode[]repartition=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
repartition[count]=r[count];
}
qSortfortime(repartition,0,this.curlen-1);
}
//直接选择排序并输出每趟排序结果
public void selectSort(){
RecordNode[]reslect=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reslect[count]=r[count];
}
RecordNode temp;
for(int i=0; i<this.curlen-1;i++) {
int min=i;
for(int j=i+1;j<this.curlen; j++)
if (reslect[j].key.compareTo(reslect[min].key)<0)
min=j;
if (min!=i) {
temp=reslect[i];
reslect[i]=reslect[min];
reslect[min]=temp;
}
System.out.print("第"+(i+1)+"趟");
display(reslect);
}
}
//直接选择排序并输出每趟排序结果//为了测试时间
public void selectSortfortime(){
RecordNode[]reslect=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reslect[count]=r[count];
}
RecordNode temp;
for(int i=0; i<this.curlen-1;i++) {
int min=i;
for(int j=i+1;j<this.curlen; j++)
if (reslect[j].key.compareTo(reslect[min].key)<0)
min=j;
if (min!=i) {
temp=reslect[i];
reslect[i]=reslect[min];
reslect[min]=temp;
}
}
}
//堆排序并输出每趟排序结果
public void sift(RecordNode[] reheap,int low, int high) {//筛选法调整堆算法
int i = low;
int j = 2 * i + 1;
RecordNode temp = reheap[i];
while (j < high) {
if (j < high - 1 && reheap[j].key.compareTo(reheap[j + 1].key) > 0)
j++;
if (temp.key.compareTo(reheap[j].key) > 0) {
reheap[i] = reheap[j];
i = j;
j = 2 * i + 1;
} else {
j = high + 1;
}
}
reheap[i] = temp;
}
void HeapSort() { //堆排序算法
RecordNode[]reheap=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reheap[count]=r[count];
}
int n = this.curlen;
RecordNode temp;
for (int i = n / 2 - 1; i >= 0; i--) { //创建堆
sift(reheap,i, n);
}
System.out.println("创建的堆为:");
display(reheap);
for (int i = n - 1; i > 0; i--) {
temp = reheap[0];
reheap[0] = reheap[i];
reheap[i] = temp;
sift(reheap,0, i);
System.out.print("第"+(n-i)+"趟");
display(reheap);
}
}
//堆排序并输出每趟排序结果//为了测试时间
public void siftfortime(RecordNode[] reheap,int low, int high) {//筛选法调整堆算法
int i = low;
int j = 2 * i + 1;
RecordNode temp = reheap[i];
while (j < high) {
if (j < high - 1 && reheap[j].key.compareTo(reheap[j + 1].key) > 0)
j++;
if (temp.key.compareTo(reheap[j].key) > 0) {
reheap[i] = reheap[j];
i = j;
j = 2 * i + 1;
} else {
j = high + 1;
}
}
reheap[i] = temp;
}
void HeapSortfortime() { //堆排序算法
RecordNode[]reheap=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
reheap[count]=r[count];
}
int n = this.curlen;
RecordNode temp;
for (int i = n / 2 - 1; i >= 0; i--) { //创建堆
siftfortime(reheap,i, n);
}
for (int i = n - 1; i > 0; i--) {
temp = reheap[0];
reheap[0] = reheap[i];
reheap[i] = temp;
siftfortime(reheap,0, i);
}
}
//归并排序
public static int countformerge=0;
public void merge(RecordNode[]r, RecordNode[] order, int h, int m, int t){//两个有序序列的归并算法
int i=h,j=m+1,k=h;
while(i<=m&&j<=t){
if(r[i].key.compareTo(r[j].key)<=0){
order[k++]=r[i++];
}else{
order[k++]=r[j++];
}
}
while (i<=m){
order[k++]=r[i++];
}
while(j<=t){
order[k++]=r[j++];
}
}
public void mergepass(RecordNode[] r, RecordNode[] order, int s, int n){//一趟归并排序
int p=0;
while(p+2*s-1<=n-1){
merge(r,order,p,p+s-1,p+2*s-1);
p+=2*s;
}
if(p+s-1<n-1){
merge(r,order,p,p+s-1,n-1);
}else{
for(int i=p;i<=n-1;i++){
order[i]=r[i];
}
}
countformerge++;//每经过一次排序进行一次打印
System.out.print("第"+countformerge+"趟");
display(order);
}
public void mergesort(){//二路归并排序(这个地方是调用的上面的函数,上面的函数调用上面的函数)
RecordNode[]remergesort=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
remergesort[count]=r[count];
}
int s=1;
int n=this.curlen;
RecordNode[] temp=new RecordNode[n];
while(s<n){
mergepass(remergesort,temp,s,n);//此时,归并完一次,排好的序列是在temp里的,而不是在remergesort里
s*=2;
mergepass(temp,remergesort,s,n);//又经过一次,在temp的序列又经过一次归并排序到了remergesort;也就是经过偶数次归并进入到remergesort里。
s*=2;
}
}
//归并排序//为了测试时间
public void mergefortime(RecordNode[]r, RecordNode[] order, int h, int m, int t){//两个有序序列的归并算法
int i=h,j=m+1,k=h;
while(i<=m&&j<=t){
if(r[i].key.compareTo(r[j].key)<=0){
order[k++]=r[i++];
}else{
order[k++]=r[j++];
}
}
while (i<=m){
order[k++]=r[i++];
}
while(j<=t){
order[k++]=r[j++];
}
}
public void mergepassfortime(RecordNode[] r, RecordNode[] order, int s, int n){//一趟归并排序
int p=0;
while(p+2*s-1<=n-1){
mergefortime(r,order,p,p+s-1,p+2*s-1);
p+=2*s;
}
if(p+s-1<n-1){
mergefortime(r,order,p,p+s-1,n-1);
}else{
for(int i=p;i<=n-1;i++){
order[i]=r[i];
}
}
}
public void mergesortfortime(){//二路归并排序(这个地方是调用的上面的函数,上面的函数调用上面的函数)
RecordNode[]remergesort=new RecordNode[this.curlen];
for(int count=0;count<curlen;count++){
remergesort[count]=r[count];
}
int s=1;
int n=this.curlen;
RecordNode[] temp=new RecordNode[n];
while(s<n){
mergepassfortime(remergesort,temp,s,n);//此时,归并完一次,排好的序列是在temp里的,而不是在remergesort里
s*=2;
mergepassfortime(temp,remergesort,s,n);//又经过一次,在temp的序列又经过一次归并排序到了remergesort;也就是经过偶数次归并进入到remergesort里。
s*=2;
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args)throws Exception {
System.out.println("首先创造一个待排序序列");
int maxSize=20;
SeqList ST=new SeqList(maxSize);
Scanner sc=new Scanner(System.in);
System.out.print("输入表格长度:");
int curlen=sc.nextInt();
if (curlen==0)
throw new Exception("无法排序");
KeyType[] k=new KeyType[curlen];
System.out.print("请输入序列:");
int[] m=new int[curlen];
for(int i=0;i<curlen;i++) {
//k[i]=new KeyType(sc.nextInt());
m[i]=sc.nextInt();
k[i]=new KeyType(m[i]);
}
for(int i=0;i<curlen;i++) {
RecordNode r=new RecordNode(k[i]);
ST.insert(ST.length(), r);
}
//接下来进行测试每一种排序
boolean flag=true;
while(flag) {
System.out.println("1:直接插入排序");
System.out.println("2:希尔排序");
System.out.println("3:改进的冒泡排序");
System.out.println("4:快速排序");
System.out.println("5:直接选择排序");
System.out.println("6:堆排序");
System.out.println("7:归并排序");
System.out.println("8:链式基数排序");
System.out.println("请输入要进行的操作");
int c = sc.nextInt();
switch (c) {
case 1:
System.out.println("原序列为:");
ST.display();
System.out.println("直接插入排序结果:");
ST.insertSort();
break;
case 2:
System.out.println("原序列为:");
ST.display();
System.out.println("希尔排序结果:");
ST.shellSort();
break;
case 3:
System.out.println("原序列为:");
ST.display();
System.out.println("改进的冒泡排序结果:");
ST.bubbleSort();
break;
case 4:
System.out.println("原序列为:");
ST.display();
System.out.println("快速排序结果:");
ST.quickSort();
break;
case 5:
System.out.println("原序列为:");
ST.display();
System.out.println("直接选择排序结果:");
ST.selectSort();
break;
case 6:
System.out.println("原序列为:");
ST.display();
System.out.println("堆排序结果:");
ST.HeapSort();
break;
case 7:
System.out.println("原序列为:");
ST.display();
System.out.println("归并排序:");
ST.mergesort();
break;
case 8:
System.out.println("原序列为:");
ST.display();
System.out.println("链式基数排序:");
//求位数
int figure=0;
for (int i=0;i<curlen;i++) {
//m[i] = m[i] > 0 ? m[i] : -m[i];
if (String.valueOf(m[i]).length()>figure){
figure=String.valueOf(m[i]).length();
}
}
Radix r = null;
try {
r = new Radix(m, figure);
} catch (Exception e) {
e.printStackTrace();
}
r.RadixAll();
break;
default:
System.out.println("输入错误");
}
System.out.println("是否继续测试,输入0结束测试,输入其他数字继续。");
int judge=sc.nextInt();
if(judge==0)
flag=false;
}
}
}
public class TimeTest {
public static void main(String[] args) {
int[] n=new int[30000];
for(int i=0;i<30000;i++){
n[i]=(int)(Math.random()*10000+1);
}
int maxSize=50000;
SeqList ST=new SeqList(maxSize);
KeyType[] k=new KeyType[30000];
int[] m=new int[30000];
for(int i=0;i<30000;i++) {
m[i]=n[i];
k[i]=new KeyType(m[i]);
}
for(int i=0;i<30000;i++) {
RecordNode r=new RecordNode(k[i]);
try {
ST.insert(ST.curlen, r);
} catch (Exception e) {
e.printStackTrace();
}
}
//以下为各种排序的时间测试
long start7 = System.currentTimeMillis();
ST.insertSortfortime();
long end7 = System.currentTimeMillis();
long timeElapsed7 = end7 - start7;
System.out.println("直接插入排序运行时间(毫秒):" + timeElapsed7);
long start6 = System.currentTimeMillis();
ST.shellSortfortime();
long end6 = System.currentTimeMillis();
long timeElapsed6 = end6 - start6;
System.out.println("希尔排序运行时间(毫秒):" + timeElapsed6);
long start2 = System.currentTimeMillis();
ST.bubbleSortfortime();
long end2 = System.currentTimeMillis();
long timeElapsed2 = end2 - start2;
System.out.println("改进的冒泡排序运行时间(毫秒):" + timeElapsed2);
long start1 = System.currentTimeMillis();
ST.quickSortfortime();
long end1 = System.currentTimeMillis();
long timeElapsed1 = end1 - start1;
System.out.println("快速排序运行时间(毫秒):" + timeElapsed1);
long start4 = System.currentTimeMillis();
ST.selectSortfortime();
long end4 = System.currentTimeMillis();
long timeElapsed4 = end4 - start4;
System.out.println("直接选择排序运行时间(毫秒):" + timeElapsed4);
long start3 = System.currentTimeMillis();
ST.HeapSortfortime();
long end3 = System.currentTimeMillis();
long timeElapsed3 = end3 - start3;
System.out.println("堆排序运行时间(毫秒):" + timeElapsed3);
long start5 = System.currentTimeMillis();
ST.mergesortfortime();
long end5 = System.currentTimeMillis();
long timeElapsed5 = end5 - start5;
System.out.println("归并排序运行时间(毫秒):" + timeElapsed5);
long start8 = System.currentTimeMillis();
RadixForTime r = null;
try {
r = new RadixForTime(m, 2);
} catch (Exception e) {
e.printStackTrace();
}
try {
r.RadixAll();
} catch (Exception e) {
e.printStackTrace();
}
long end8 = System.currentTimeMillis();
long timeElapsed8 = end8 - start8;
System.out.println("链式基数排序运行时间(毫秒):" + timeElapsed8);
}
}
五、运行结果
1.Test
2.TimeTest