-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
merge_sort.cpp
122 lines (109 loc) · 2.13 KB
/
merge_sort.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
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
#include <iostream>
struct Node {
int data;
Node * next;
Node ( int d ) : data{ d }, next{ nullptr } { }
};
void printList( Node * head ) {
while( head ) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "NULL" << std::endl;
}
void push( Node * & head, int data )
{
Node * newNode = new Node(data);
if ( head == nullptr ) {
head = newNode;
} else {
Node * temp = head;
while ( temp->next != nullptr ) {
temp = temp->next;
}
temp->next = newNode;
}
}
void splitList( Node * head, Node * & list1, Node * & list2 )
{
if ( head == nullptr || head->next == nullptr ) {
list1 = head;
list2 = nullptr;
return;
}
Node * fast = head->next;
Node * slow = head;
while( fast ) {
fast = fast->next;
if( fast ) {
slow = slow->next;
fast = fast->next;
}
}
list1 = head;
list2 = slow->next;
slow->next = nullptr;
}
void moveNode( Node * & dstNode, Node * & srcNode )
{
if (srcNode == nullptr) {
return;
}
Node * nextNode = srcNode->next;
dstNode->next = srcNode;
dstNode = srcNode;
srcNode = nextNode;
}
Node * sortedMerge(Node * list1, Node * list2)
{
if (list1 == nullptr) {
return list2;
}
if (list2 == nullptr) {
return list1;
}
Node dummy(0);
Node * tailNode = &dummy;
dummy.next = nullptr;
while( list1 && list2 ) {
if (list1->data <= list2->data){
moveNode(tailNode, list1);
} else {
moveNode(tailNode, list2);
}
}
while(list2) {
moveNode(tailNode, list2);
}
while(list1) {
moveNode(tailNode, list1);
}
return (dummy.next);
}
void mergeSort( Node * & head ) {
if (head == nullptr || head->next == nullptr) {
return;
}
Node * list1 = nullptr;
Node * list2 = nullptr;
splitList(head, list1, list2);
mergeSort(list1);
mergeSort(list2);
head = sortedMerge(list1, list2);
}
int main()
{
Node * head = nullptr;
push(head, 5);
push(head, 1);
push(head, 3);
push(head, 6);
push(head, 4);
push(head, 2);
std::cout << "Before Sorting :";
printList(head);
mergeSort(head);
std::cout << "After Sorting :";
printList(head);
return 1;
}