完整代码
https://github.com/gongfpp/sortsWithQt
成品(随机生成数字后全过了一遍)
一、 实验任务(实验题目、目的)
实现5种排序(冒泡、希尔、堆排、基数、快排),并对比时间效率
二、 任务分析
- 逐一实现各排序算法
- 设计主窗口与5个排序按键和一个总排序按键
- 设置各按钮槽函数
- 生成随机数据以供排序
- 设计数据输出
- 将排序算法和gui图形界面融合
三、 实验设计(实验环境、算法、步骤、核心代码等)
环境:QT5 使用QT CREATOR编程(内置帮助快速查询,很方便)
.h声明如下,大体为以下框架
各算法具体实现有点老生常谈了,略
private slots:
void on_creatNumber_clicked();
void on_quickSort_clicked();
void on_bubbleSort_clicked();
void on_shellSort_clicked();
void on_heapSort_clicked();
void on_radixSort_clicked();
void on_allSort_clicked();
private:
void bubbleSort(QVector<int>& bubbleVec );
void quickSort(QVector<int>* ,int ,int );
void shellSort (QVector<int>& shellVec);
void heapSort (QVector<int>& heapVec);
void radixSort (QVector<int>& radixVec);
总排序按钮槽函数如下,以此类比其他槽函数的输出和结构
void MainWindow::on_allSort_clicked()
{
if(vec.isEmpty()){
QMessageBox::warning(this,"No data Waning","Creat data first,and then sort");
return ;
}
QVector<QVector<int> > allSortVecs(5,QVector<int>(vec));
ui->currentTime->setText("");
ui->sortedEdit->setText("");
QTime time;
time.start();
QTime timeLite;
timeLite.start();
bubbleSort(allSortVecs[0]);
ui->currentTime->setText("冒泡耗时:"+QString::number(timeLite.elapsed()));
ui->allTime->append("冒泡耗时:"+QString::number(timeLite.elapsed()));
timeLite.restart();
quickSort(&allSortVecs[1],0,allSortVecs[1].size()-1);
ui->currentTime->setText("快排耗时:"+QString::number(timeLite.elapsed()));
ui->allTime->append("快排耗时:"+QString::number(timeLite.elapsed()));
timeLite.restart();
shellSort(allSortVecs[2]);
ui->currentTime->setText("希尔耗时:"+QString::number(timeLite.elapsed()));
ui->allTime->append("希尔耗时:"+QString::number(timeLite.elapsed()));
timeLite.restart();
heapSort(allSortVecs[3]);
ui->currentTime->setText("堆排耗时:"+QString::number(timeLite.elapsed()));
ui->allTime->append("堆排耗时:"+QString::number(timeLite.elapsed()));
timeLite.restart();
radixSort(allSortVecs[4]);
ui->currentTime->setText("基数耗时:"+QString::number(timeLite.elapsed()));
ui->allTime->append("基数耗时:"+QString::number(timeLite.elapsed()));
ui->currentTime->setText("总耗时:"+QString::number(time.elapsed()));
ui->allTime->append("总耗时:"+QString::number(time.elapsed()));
}
四、 实验结果
qt输出到文本框比较慢,数据大了比较卡
生成的数据大小默认为50000个,可在.h中的
int numberSize = 50000;
进行修改
五、 总结
冒泡排序最好写,可在数据量大的情况下最慢,快排最快
完整代码可免费在此下载
https://github.com/gongfpp/sortsWithQt