-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.cpp
More file actions
101 lines (95 loc) · 3 KB
/
loop.cpp
File metadata and controls
101 lines (95 loc) · 3 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "loop.h"
Loop::Loop(QObject *parent) : QObject(parent)
{
Sequence = new GenerateSequence();
SortedSequence = new Sort();
TempFile = new QFile("temp.txt");
if(TempFile->exists()) TempFile->remove();
}
void Loop::on_CurrentProgress_change(double Value)
{
emit CurrentProgress_change(Value);
}
int Loop::Compute(int ElementsCount, int LoopTimes, int SortType, int SequenceType)
{
QObject::connect(SortedSequence,&Sort::CurrentProgress_change,this,&Loop::on_CurrentProgress_change);
emit OverallProgress_reset();
if(TempFile->exists()) TempFile->remove();
WriteLine("Elements Time");
QThread::currentThread()->msleep(500);
int Interval = ElementsCount/LoopTimes;
double OverallProgress = 100.0/LoopTimes;
Elements.resize(LoopTimes);
Time.resize(LoopTimes);
for(int i = Interval; i<ElementsCount; i+=Interval){
GenerateAndSort(i, SortType, SequenceType);
OverallProgress_change(OverallProgress);
emit CurrentProgress_reset();
WriteLine(QString::number(i)+" "+QString::number(SortedSequence->TimeInSec));
Elements[i/Interval-1] = i;
Time[i/Interval-1] = SortedSequence->TimeInSec;
}
emit CurrentProgress_reset();
GenerateAndSort(ElementsCount, SortType, SequenceType);
OverallProgress_change(OverallProgress);
WriteLine(QString::number(ElementsCount)+" "+QString::number(SortedSequence->TimeInSec));
Elements[LoopTimes-1] = ElementsCount;
Time[LoopTimes-1] = SortedSequence->TimeInSec;
QThread::currentThread()->msleep(1500);
emit Vectors_compute(Elements, Time);
emit OverallProgress_reset();
emit CurrentProgress_reset();
emit Finished();
return 0;
}
void Loop::GenerateAndSort(int ElementsCount, int SortType, int SequenceType)
{
switch (SequenceType) {
case 0:
Sequence->GenerateIncreasing(ElementsCount);
break;
case 1:
Sequence->GenerateDecreasing(ElementsCount);
break;
case 2:
Sequence->GenerateConstant(ElementsCount);
break;
case 3:
Sequence->GenerateRandom(ElementsCount);
break;
case 4:
Sequence->GenerateAShaped(ElementsCount);
break;
default:
break;
}
switch (SortType) {
case 0:
SortedSequence->SelectionSort(Sequence->Value, ElementsCount);
break;
case 1:
SortedSequence->InsertionSort(Sequence->Value, ElementsCount);
break;
case 2:
SortedSequence->ShellSort(Sequence->Value, ElementsCount);
break;
case 3:
SortedSequence->QuickSortRight(Sequence->Value, ElementsCount);
break;
case 4:
SortedSequence->QuickSortRandom(Sequence->Value, ElementsCount);
break;
case 5:
SortedSequence->HeapSort(Sequence->Value, ElementsCount);
break;
default:
break;
}
}
void Loop::WriteLine(QString Line)
{
if (!TempFile->open(QIODevice::Append)) return;
QTextStream Stream(TempFile);
Stream << Line << endl;
TempFile->close();
}