-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMergeSort_MultiThread.cpp
More file actions
205 lines (167 loc) · 5.47 KB
/
MergeSort_MultiThread.cpp
File metadata and controls
205 lines (167 loc) · 5.47 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//HW1 by Wanxiang Xie
//SU Net ID: wxie15 SUID: 408358088
//Due: 11:59PM, Friday(1/31)
/*
Implement the two member functions: merge_sort and merge, as defined below for a sequential merge sort.
Note that the merge will be called by merge_sort.
In implementing both functions, you are only allowed to modify "next" and "previous" of nodes, but not "values" of nodes.
You are not allowed to use any external structures such as array, linked list, etc.
You are not allowed to create any new node.
You are not allowed to create any new function.
After completing the above sequential version, create a parallel version, by using two additional threads to speed up the merge sort.
You have to use the two functions you have implemented above. You are not allowed to create new functions. Extra work will be needed in main function.
In your threaded implementation, you are allowed to introduce an extra node and a global pointer to the node.
It is alright if your implementation does not require the extra node or global pointer to node.
*/
#include <iostream>
#include <thread>
using namespace std;
class node {
public:
int value;
node* next;
node* previous;
node(int i) { value = i; next = previous = nullptr; }
node() { next = previous = nullptr; }
};
class doubly_linked_list {
public:
int num_nodes;
node* head;
node* tail;
doubly_linked_list() { num_nodes = 0; head = tail = nullptr; }
void make_random_list(int m, int n);
void print_forward();
void print_backward();
//Recursively merge sort i numbers starting at node pointed by p
void merge_sort(node* p, int i);//in-place recursive merge sort
//Merge i1 numbers starting at node pointed by p1 with i2 numbers
//starting at node pointed by p2
void merge(node* p1, int i1, node* p2, int i2);
};
void doubly_linked_list::make_random_list(int m, int n) {
for (int i = 0; i < m; i++) {
node* p1 = new node(rand() % n);
p1->previous = tail;
if (tail != nullptr) tail->next = p1;
tail = p1;
if (head == nullptr) head = p1;
num_nodes++;
}
}
void doubly_linked_list::print_forward() {
cout << endl;
node* p1 = head;
while (p1 != nullptr) {
cout << p1->value << " ";
p1 = p1->next;
}
}
void doubly_linked_list::print_backward() {
cout << endl;
node* p1 = tail;
while (p1 != nullptr) {
cout << p1->value << " ";
p1 = p1->previous;
}
}
void doubly_linked_list::merge_sort(node* p, int i)
{
if (p == nullptr || p->next == nullptr || i == 0 || i == 1) return;
int a = i / 2;
int b = i - a;
node* p1 = p;
node* p2 = p;
node* mid = p;
for (int n = 0; n != a - 1; ++n) {
mid = mid->next;
}
p2 = mid->next;
p2->previous = nullptr;
mid->next = nullptr;
merge_sort(p1, a);
merge_sort(p2, b);
//assign head to p1 p2
while (p1->previous) p1 = p1->previous;
while (p2->previous) p2 = p2->previous;
merge(p1, a, p2, b);
//p = head;
}
void doubly_linked_list::merge(node* p1, int i1, node* p2, int i2)
{
if (p1->value <= p2->value) {
head = p1;
p1->previous = nullptr;
p1 = p1->next;
}
else
{
head = p2;
p2->previous = nullptr;
p2 = p2->next;
}
node* cur = head;
while (p1 && p2) {
if (p1->value <= p2->value) {
cur->next = p1;
p1->previous = cur;
p1 = p1->next;
}
else
{
cur->next = p2;
p2->previous = cur;
p2 = p2->next;
}
cur = cur->next;
}
if (p1 != nullptr) {
cur->next = p1;
p1->previous = cur;
}
if (p2 != nullptr) {
cur->next = p2;
p2->previous = cur;
}
node* p_tail = head;
while (p_tail->next) {
p_tail = p_tail->next;
}
tail = p_tail;
}
int main() {
/*
Implement the merge_sort and merge_functions defined above to complete a sequential version of bove to complete a sequential version of
merge sort.
*/
doubly_linked_list d1, d2;
d1.make_random_list(30, 20);
d1.print_forward();
d1.print_backward();
d1.merge_sort(d1.head, d1.num_nodes);
d1.print_forward();
d1.print_backward();
d2.make_random_list(50, 40);
d2.print_forward();
d2.print_backward();
/*
Create two additional threads to speed up the merge sort.
You have to still use the same merge_sort and merge functions implemented above.
You will need to do some extra work within main funciton.
*/
node* init_head = d2.head;
node* mid_head = d2.head;
for (int i = 0; i < d2.num_nodes / 2; i++) mid_head = mid_head->next;
mid_head->previous->next = nullptr;
mid_head->previous = nullptr;
thread t1{ &doubly_linked_list::merge_sort, ref(d2) ,init_head,d2.num_nodes / 2 };
thread t2{ &doubly_linked_list::merge_sort, ref(d2),mid_head,d2.num_nodes - d2.num_nodes / 2 };
if (t1.joinable()) t1.join();
if (t2.joinable()) t2.join();
while (init_head->previous) init_head = init_head->previous;
while (mid_head->previous) mid_head = mid_head->previous;
d2.merge(init_head, d2.num_nodes / 2, mid_head, d2.num_nodes - d2.num_nodes / 2);
d2.print_forward();
d2.print_backward();
return 0;
}