做网站如何分类产品,企业网站设计沈阳,网站图片多大,html网站分页怎么做的1.相关描述 随机生产1000个数字#xff0c;然后进行冒泡排序与快速排序。随机生成类继承QThread类、冒泡排序使用moveToThread方法添加到一个线程中、快速排序类继承QRunnable类#xff0c;添加到线程池中进行排序。 2.相关界面 3.相关代码 widget.cpp #include widget…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 QThreadPoolWidget::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, [](QVectorint 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, [](QVectorint list){for(int i 0; i list.size(); i){ui-listWidgetBubbleSort-addItem(QString::number(list[i]));}});connect(quicksort, TQuickSort::sendList, this, [](QVectorint 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 QThreadclass TGenerateNum : public QThread
{Q_OBJECT
public:TGenerateNum(QObject *parentnullptr);void setCnt(qint32 cnt);// QThread interface
protected:void run() override;
signals:void sendList(QVectorint list);private:qint32 m_cnt;
};
#endif // TGENERATENUM_H tgeneratenum.cpp #include tgeneratenum.h
#include QRandomGenerator
#include QVector
#include QElapsedTimer
#include QDebugTGenerateNum::TGenerateNum(QObject *parent):QThread(parent) {}void TGenerateNum::setCnt(qint32 cnt)
{m_cnt cnt;
}void TGenerateNum::run()
{qDebug() 生成随机数线程地址: QThread::currentThread();QElapsedTimer time;time.start();QVectorint 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 QObjectclass TBubbleSort : public QObject
{Q_OBJECT
public:explicit TBubbleSort(QObject *parent nullptr);void working(QVectorint list);
signals:void sendList(QVectorint list);
private:void bubbleSort(QVectorint list);QVectorint m_list;
};#endif // TBUBBLESORT_H tbubblesort.cpp #include tbubblesort.h
#include QElapsedTimer
#include QDebug
#include QThreadTBubbleSort::TBubbleSort(QObject *parent): QObject{parent}
{}void TBubbleSort::working(QVectorint list)
{qDebug() 冒泡线程地址: QThread::currentThread();QElapsedTimer time;time.start();this-bubbleSort(list);int milsec time.elapsed();qDebug() 冒泡排序总共用时 milsec 毫秒;emit sendList(list);
}void TBubbleSort::bubbleSort(QVectorint 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 QRunnableclass TQuickSort : public QObject, public QRunnable
{Q_OBJECT
public:explicit TQuickSort(QObject *parent nullptr);void setList(QVectorint list);
signals:void sendList(QVectorint list);// QRunnable interface
public:void run() override;
private:void quick_sort(QVectorint list, int l, int r);QVectorint m_list;
};#endif // TQUICKSORT_Htquicksort.cpp #include tquicksort.h
#include QElapsedTimer
#include QDebug
#include QThread
TQuickSort::TQuickSort(QObject *parent): QObject{parent}, QRunnable()
{// 开启自动回收由线程池回收对象资源setAutoDelete(true);
}void TQuickSort::setList(QVectorint 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(QVectorint list, int l, int r){//如果小于等于1个数据元素·直接返回结束快排函数 r为数组元素总个数if(l1 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, first1, r);
}