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
|
namespace CLRS
{
template <typename T>
void merge(T *array, std::size_t p, std::size_t q, std::size_t r)
{
std::size_t n1 = q - p + 1;
std::size_t n2 = r - q;
T larray[n1], rarray[n2];
for(std::size_t i = 0; i < n1; ++i)
larray[i] = array[p + i];
for(std::size_t j = 0; j < n2; ++j)
rarray[j] = array[q + j + 1];
std::size_t i = 0, j = 0, k = p;
while(k <= r)
{
if(i < n1 && j == n2)
{
array[k] = larray[i];
++i;
}
else if(i == n1 && j < n2)
{
array[k] = rarray[j];
++j;
}
else if(larray[i] <= rarray[j])
{
array[k] = larray[i];
++i;
}
else
{
array[k] = rarray[j];
++j;
}
++k;
}
}
template <typename T>
void merge_sort(T *array, std::size_t p, std::size_t r)
{
if(p < r)
{
std::size_t q = (p + r) / 2;
merge_sort(array, p, q);
merge_sort(array, q + 1, r);
merge(array, p, q, r);
}
}
}
|