1.相关描述
随机生产1000个数字,然后进行冒泡排序与快速排序。随机生成类继承QThread类、冒泡排序使用moveToThread方法添加到一个线程中、快速排序类继承QRunnable类,添加到线程池中进行排序。
2.相关界面
3.相关代码
widget.cpp
#include "widget.h" #include "ui_widget.h" #include "tgeneratenum.h" #include "tbubblesort.h" #include "tquicksort.h" #include <QDebug> #include <QThreadPool>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);qDebug() << "主线程:" << QThread::currentThread();/******************** 多线程的使用方法一 ****************/TGenerateNum *gen = new TGenerateNum; // 继承QThread类/******************* 多线程的使用方法二 **********************/QThread *bubbleThread = new QThread;TBubbleSort *bubble = new TBubbleSort;bubble->moveToThread(bubbleThread);/******************* 多线程的使用方法三 线程池的使用 **********************/TQuickSort *quicksort = new TQuickSort;QThreadPool::globalInstance()->setMaxThreadCount(10);// 按钮的信号槽connect(ui->btnStart, &QPushButton::clicked, this, [=](){gen->setCnt(10000);gen->start();});// 生成随机数connect(gen, &TGenerateNum::sendList, this, [=](QVector<int> list){quicksort->setList(list);QThreadPool::globalInstance()->start(quicksort);bubbleThread->start(); // 开启冒泡排序线程for(int i = 0; i < list.size(); i++){ui->listWidgetGenerate->addItem(QString::number(list[i]));}});connect(gen, &TGenerateNum::sendList, bubble, &TBubbleSort::working);connect(bubble, &TBubbleSort::sendList, this, [=](QVector<int> list){for(int i = 0; i < list.size(); i++){ui->listWidgetBubbleSort->addItem(QString::number(list[i]));}});connect(quicksort, &TQuickSort::sendList, this, [=](QVector<int> list){for(int i = 0; i < list.size(); i++){ui->listWidgetQuickSort->addItem(QString::number(list[i]));}});// 释放内存connect(this, &Widget::destroyed, this, [=](){gen->quit();gen->wait();gen->deleteLater();bubbleThread->quit();bubbleThread->wait();bubbleThread->deleteLater();bubble->deleteLater();}); }Widget::~Widget() {delete ui; }
生成随机数:
tgeneratenum.h
#ifndef TGENERATENUM_H #define TGENERATENUM_H#include <QObject> #include <QThread>class TGenerateNum : public QThread {Q_OBJECT public:TGenerateNum(QObject *parent=nullptr);void setCnt(qint32 cnt);// QThread interface protected:void run() override; signals:void sendList(QVector<int> list);private:qint32 m_cnt; }; #endif // TGENERATENUM_H
tgeneratenum.cpp
#include "tgeneratenum.h" #include <QRandomGenerator> #include <QVector> #include <QElapsedTimer> #include <QDebug>TGenerateNum::TGenerateNum(QObject *parent):QThread(parent) {}void TGenerateNum::setCnt(qint32 cnt) {m_cnt = cnt; }void TGenerateNum::run() {qDebug() << "生成随机数线程地址:" << QThread::currentThread();QElapsedTimer time;time.start();QVector<int> list;for(int i = 0; i < m_cnt; i++){int num = QRandomGenerator::global()->bounded(100000);list.push_back(num);}int milsec = time.elapsed();qDebug() << "生成" << m_cnt << "个随机数总共用时:" << milsec << "毫秒";emit sendList(list); }
生成随机数,需要加时间种子
冒泡排序:
tbubblesort.h
#ifndef TBUBBLESORT_H #define TBUBBLESORT_H#include <QObject>class TBubbleSort : public QObject {Q_OBJECT public:explicit TBubbleSort(QObject *parent = nullptr);void working(QVector<int> list); signals:void sendList(QVector<int> list); private:void bubbleSort(QVector<int>& list);QVector<int> m_list; };#endif // TBUBBLESORT_H
tbubblesort.cpp
#include "tbubblesort.h" #include <QElapsedTimer> #include <QDebug> #include <QThread>TBubbleSort::TBubbleSort(QObject *parent): QObject{parent} {}void TBubbleSort::working(QVector<int> list) {qDebug() << "冒泡线程地址:" << QThread::currentThread();QElapsedTimer time;time.start();this->bubbleSort(list);int milsec = time.elapsed();qDebug() << "冒泡排序总共用时:" << milsec << "毫秒";emit sendList(list); }void TBubbleSort::bubbleSort(QVector<int> &list) {for (int i = 0; i < list.size(); i++){for (int j = 1; j < list.size()-i; j++){if (list[j - 1] > list[j]){int temp = list[j - 1];list[j - 1] = list[j];list[j] = temp;}}} }
快速排序:
tquicksort.h
#ifndef TQUICKSORT_H #define TQUICKSORT_H#include <QObject> #include <QRunnable>class TQuickSort : public QObject, public QRunnable {Q_OBJECT public:explicit TQuickSort(QObject *parent = nullptr);void setList(QVector<int> list); signals:void sendList(QVector<int> list);// QRunnable interface public:void run() override; private:void quick_sort(QVector<int>& list, int l, int r);QVector<int> m_list; };#endif // TQUICKSORT_H
tquicksort.cpp
#include "tquicksort.h" #include <QElapsedTimer> #include <QDebug> #include <QThread> TQuickSort::TQuickSort(QObject *parent): QObject{parent}, QRunnable() {// 开启自动回收,由线程池回收对象资源setAutoDelete(true); }void TQuickSort::setList(QVector<int> list) {m_list = list; }void TQuickSort::run() {qDebug() << "快排线程地址:" << QThread::currentThread();QElapsedTimer time;time.start();this->quick_sort(m_list, 0, m_list.size());int milsec = time.elapsed();qDebug() << "快速排序总共用时:" << milsec << "毫秒";emit sendList(m_list); }void TQuickSort::quick_sort(QVector<int>& list, int l, int r){//如果小于等于1个数据元素·直接返回结束快排函数 r为数组元素总个数if(l+1 >= r){return ;}int first = l, last = r-1, key = list[first];while(first < last){while(first < last && list[last] >= key){--last;}//如果值小于 key分界值 交换list[first] = list[last];while(first < last && list[first] < key){++first;}//如果值大于key分界值 交换list[last] = list[first];}list[first] = key;//递归左右部分进行快排quick_sort(list, l, first);quick_sort(list, first+1, r); }