-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.cpp
72 lines (59 loc) · 1.54 KB
/
mergesort.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
62
63
64
65
66
67
68
69
70
71
72
/* A possible implementation of mergesort in C++ */
#include <iostream>
using namespace std;
struct Elem {
int data;
Elem* next;
};
void split(Elem*& list1, Elem*& list2) {
/* A possible implementation of split function is assigning the elements in odd position to list2 */
if (list1 == nullptr || list1->next == nullptr)
return;
Elem* l = list1->next;
list1->next = l->next;
l->next = list2;
list2 = l;
split(list1->next, list2);
}
void merge(Elem*& list1, Elem* list2) {
if (list1 == nullptr) {list1 = list2; return;}
if (list2 == nullptr) {return;}
if (list1->data <= list2->data) {
merge(list1->next, list2);
}
else {
merge(list2->next, list1);
list1 = list2;
}
}
void mergesort(Elem*& list1) {
if (list1 == nullptr || list1->next == nullptr)
return;
Elem* list2 = nullptr;
/* split list1 into two lists and assign the second half to list2 */
split(list1, list2);
/* order the first list */
mergesort(list1);
/* order the second list */
mergesort(list2);
/* merge the two lists */
merge(list1, list2);
}
void insertElem(Elem*& list1, int x) {
Elem* p = new Elem;
p->data = x;
p->next = list1;
list1 = p;
}
int main() {
Elem* head = nullptr;
insertElem(head, 4);
insertElem(head, 6);
insertElem(head, 1);
insertElem(head, 2);
mergesort(head);
for (Elem* p = head; p != nullptr; p = p->next) {
cout << p->data << " ";
}
return 0;
}