-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge.cpp
61 lines (55 loc) · 1.32 KB
/
Merge.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
58
59
60
61
// Merge two lists
//
#include <cstdlib>
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <thread>
#include <time.h>
#include "mergeSort.h"
//
// Globals
//
extern control_block cb;
//
// Merge two lists on the interval (l0:l1), that is,
// (l0:mid-1) and (mid-1:n1), where mid = l0 + n/2
// This is a serial implementation Serial
//
void Merge(std::vector<int> *keysIn, std::vector<int> *keysOut,
int l0, int l1){
int n = (l1-l0) + 1;
int mid = l0 + n/2;
int min1 = l0, max1 = mid-1;
int min2 = mid, max2 = l1;
#ifdef DEBUG
// Output the keys if N is <= 16
if (n <= 16){
for (int i=min1;i<=max1;i++)
std::cout<<(*keysIn)[i]<<" ";
std::cout<<std::endl;
for (int i=min2;i<=max2;i++)
std::cout<<(*keysIn)[i]<<" ";
std::cout<<std::endl;
}
#endif
int l=min1;
int r=min2;
int i;
for (i=0; i < max2-min1+1 ; i++) {
if ((*keysIn)[l]<(*keysIn)[r]) {
(*keysOut)[i+min1]=(*keysIn)[l++];
if (l>max1) break;
} else {
(*keysOut)[i+min1]=(*keysIn)[r++];
if (r>max2) break;
}
}
while (l<=max1) {
i++;
(*keysOut)[i+min1]=(*keysIn)[l++];
}
while (r<=max2) {
i++;
(*keysOut)[i+min1]=(*keysIn)[r++];
}
}