-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblethread.cpp
52 lines (43 loc) · 1.06 KB
/
bubblethread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "bubblethread.h"
BubbleThread::BubbleThread()
{
}
BubbleThread::~BubbleThread()
{
delete [] heights;
}
//设置属性值
void BubbleThread::setAttr(int number, int *heights, int speed)
{
this->number = number;
this->heights = heights;
this->speed = speed;
}
//设置速度
void BubbleThread::setSpeed(int speed)
{
this->speed = speed;
}
void BubbleThread::run()
{
//冒泡排序具体实现
for(int i = number - 1; i > 0; i--)
{
for(int j = 0; j < i; j++)
{
//相邻两元素逆序则交换
if(heights[j] > heights[j + 1])
{
int tmp = heights[j];
heights[j] = heights[j + 1];
heights[j + 1] = tmp;
//一次交换之后就发送信号,通知主线程更新界面
emit returnHeights(j, j+1, heights);
//小憩一下,此段时间让主线程更新界面
msleep(speed);
}
}
}
//排序结束,发送结束的信号
emit sortFinish(0);
}