-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergeSorter.cpp
57 lines (44 loc) · 1.17 KB
/
MergeSorter.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
53
54
55
56
57
#include "MergeSorter.h"
MergeSorter::MergeSorter()
{
}
void MergeSorter::startSortingThread(){
mergeSort(vector,0,vector->size()-1);
}
void MergeSorter::mergeSort(QVector<int> *vector, int first, int last){
if(isInterruptionRequested())
return;
if(last>first){
int middle = (first+last)/2;
mergeSort(vector, first, middle);
mergeSort(vector, middle+1, last);
merge(vector, first,last, middle);
}
}
void MergeSorter::merge(QVector<int> *vector, int first, int last, int middle){
QVector<int> v1(middle-first+1);
QVector<int> v2(last-middle);
for(int i=0;i<middle-first+1;i++){
v1[i] = vector->at(i+first);
}
for(int i=0;i< last-middle;i++){
v2[i] = vector->at(i+middle+1);
}
int i=0;
int j=0;
int p=first;
while( i<v1.size() || j<v2.size() ){
if(isInterruptionRequested())
return;
if( j==v2.size() || (i<v1.size() && v1.at(i) < v2.at(j) ) ){
(*vector)[p] = v1[i];
i++;
}else{
(*vector)[p] = v2[j];
j++;
}
emit vectorChanged(p);
waitInterval();
p++;
}
}