-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadMergeV2.hpp
95 lines (84 loc) · 2.57 KB
/
threadMergeV2.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using namespace std;
void mergeThreadV2(int *array, int mid, int end)
{
int tempBegin = 0;
mid++;
int tempMid = mid;
int *mergedArray = new int[end + 1];
end++;
int mergedArrayIndex = 0;
int reverseMergedArrayIndex = end - 1; // last index of merged array
int leftMid = mid / 2;
int rightMid = mid + leftMid;
int tempEndLeft = mid - 1;
int tempEndRight = end - 1;
while (tempBegin <= tempEndLeft || tempMid <= tempEndRight)
{
if (array[tempEndLeft] >= array[tempEndRight])
{
mergedArray[reverseMergedArrayIndex] = array[tempEndLeft];
tempEndLeft--;
}
else
{
mergedArray[reverseMergedArrayIndex] = array[tempEndRight];
tempEndRight--;
}
reverseMergedArrayIndex--;
if (tempBegin <= tempEndLeft && tempMid <= tempEndRight)
{
if (array[tempBegin] <= array[tempMid])
{
mergedArray[mergedArrayIndex] = array[tempBegin];
tempBegin++;
}
else
{
mergedArray[mergedArrayIndex] = array[tempMid];
tempMid++;
}
}
else if (tempBegin <= tempEndLeft)
{
mergedArray[mergedArrayIndex] = array[tempBegin++];
}
else if (tempMid <= tempEndRight)
{
mergedArray[mergedArrayIndex] = array[tempMid++];
}
mergedArrayIndex++;
}
for (size_t i = 0; i < end; i++)
{
array[i] = mergedArray[i];
}
}
void mergeSortThreadV2(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 > 10000)
{
threadCount++;
thread sortThread(mergeSortThreadV2, array, begin, mid);
thread sortThread2(mergeSortThreadV2, array, mid + 1, end);
mid = mid - begin;
end = end - begin;
sortThread.join();
sortThread2.join();
mergeThreadV2(&array[begin], mid, end);
}
else
{
mergeSortThreadV2(array, begin, mid);
mergeSortThreadV2(array, mid + 1, end);
mid = mid - begin;
end = end - begin;
mergeThreadV2(&array[begin], mid, end);
}
}