-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadMergeV1.hpp
79 lines (69 loc) · 1.97 KB
/
threadMergeV1.hpp
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
using namespace std;
void mergeThreadV1(int *array, int mid, int end)
{
int tempBegin = 0;
mid++;
int tempMid = mid;
int *mergedArray = new int[end + 1];
end++;
int mergedArrayIndex = 0;
while (tempBegin < mid || tempMid < end)
{
if (tempBegin < mid && tempMid < end)
{
if (array[tempBegin] <= array[tempMid])
{
mergedArray[mergedArrayIndex] = array[tempBegin];
tempBegin++;
}
else
{
mergedArray[mergedArrayIndex] = array[tempMid];
tempMid++;
}
}
else if (tempBegin < mid)
{
mergedArray[mergedArrayIndex] = array[tempBegin++];
}
else if (tempMid < end)
{
tempMid++;
continue;
}
mergedArrayIndex++;
}
for (size_t i = 0; i < mergedArrayIndex; i++)
{
array[i] = mergedArray[i];
}
}
void mergeSortThreadV1(int array[], int begin, int end)
{
if (begin >= end)
return;
int arrayCount = end - begin;
auto mid = begin + (end - begin) / 2;
int threadPower = arrayCount / threadCount;
// if you have powerfull computer then you need to bigger count
// at least 1000 number otherwise logical to single thread dont need multithread
if (threadPower > 11000)
{
threadCount++;
thread sortThread(mergeSortThreadV1, array, begin, mid);
thread sortThread2(mergeSortThreadV1, array, mid + 1, end);
mid = mid - begin;
end = end - begin;
sortThread.join();
sortThread2.join();
mergeThreadV1(&array[begin], mid, end);
}
else
{
mergeSortThreadV1(array, begin, mid);
mergeSortThreadV1(array, mid + 1, end);
mid = mid - begin;
end = end - begin;
mergeThreadV1(&array[begin], mid, end);
}
}