-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.cpp
118 lines (101 loc) · 2.52 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
#include <iostream>
using namespace std;
class Node{
public:
int val;
Node* next;
Node(int data = 0){
this->val = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted: "<<this->val<<endl;
this->next = NULL;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;
}
// Finds the middle node
Node* findMid(Node* &head){
Node* slow = head;
Node* fast = head->next;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
// Iterative Apporach to merge two sorted LLs
Node* mergeTwoLists(Node* &list1, Node* &list2) {
if(list1 == NULL || list2 == NULL){
return list1 == NULL ? list2 : list1;
}
Node* ansHead = new Node(-1);
Node* ansTail = ansHead;
while(list1 && list2){
if(list1->val <= list2->val){
ansTail->next = list1;
ansTail = list1;
list1 = list1->next;
}
else{
ansTail->next = list2;
ansTail = list2;
list2 = list2->next;
}
}
if(list1)
ansTail->next = list1;
if(list2)
ansTail->next = list2;
return ansHead->next;
}
Node* mergeSort(Node* &head){
if(head == NULL || head->next == NULL)
return head;
// Break LL into 2 halves using mid node
Node* mid = findMid(head);
Node* left = head;
Node* right = mid->next;
mid->next = NULL;
// Make sort RE calls
left = mergeSort(left);
right = mergeSort(right);
// merge both left & right sorted LLs
Node* ans = mergeTwoLists(left,right);
return ans;
}
int main()
{
Node* head = new Node(121);
Node* sec = new Node(2);
Node* third = new Node(33);
Node* forth = new Node(16);
Node* fifth = new Node(55);
Node* sixth = new Node(69);
Node* seventh = new Node(26);
Node* eighth = new Node(90);
Node* ninth = new Node(-23);
Node* tenth = new Node(689);
head->next = sec;
sec->next = third;
third->next = forth;
forth->next = fifth;
fifth->next = sixth;
sixth->next = seventh;
seventh->next = eighth;
eighth->next = ninth;
ninth->next = tenth;
tenth->next = NULL;
print(head);
Node* ansHead = mergeSort(head);
print(ansHead);
return 0;
}