-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
134 lines (117 loc) · 2.36 KB
/
MergeSort.cpp
File metadata and controls
134 lines (117 loc) · 2.36 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
// Github username: kashika1109
// Aim: Merge sort using Linked List in C++
// Date: 10 October 2022
//{ Driver Code Starts
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
// } Driver Code Ends
/* Structure of the linked list node is as
struct Node
{
int data;
struct Node* next;
Node(int x) { data = x; next = NULL; }
};
*/
class Solution{
public:
//Function to sort the given linked list using Merge Sort.
Node* midpoint(Node *head){ //MIDPOINT OF LINKED LIST
Node *slow=head;
Node *fast=head->next;
while(fast!=NULL && fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
Node* merge(Node *h1,Node *h2){ //MERGING TWO LINKED LIST
if(h1==NULL)
return h2;
if(h2==NULL)
return h1;
Node *fhead=NULL;
Node *ftail=NULL;
if(h1->data <= h2->data){
fhead=h1;
ftail=h1;
h1=h1->next;
}
else{
fhead=h2;
ftail=h2;
h2=h2->next;
}
while(h1!=NULL && h2!=NULL){
if(h1->data <= h2->data){
ftail->next=h1;
ftail=h1;
h1=h1->next;
}
else{
ftail->next=h2;
ftail=h2;
h2=h2->next;
}
}
if(h1==NULL){
ftail->next=h2;
} else if(h2==NULL){
ftail->next=h1;
}
return fhead;
}
Node* mergeSort(Node *head){ //MERGESORT
if(head==NULL || head->next==NULL) return head;
Node *mid=midpoint(head);
Node *head1=head;
Node *head2=mid->next;
mid->next=NULL;
Node *left_LL=mergeSort(head1);
Node *right_LL=mergeSort(head2);
Node *finalHead=merge(left_LL,right_LL);
return finalHead;
}
};
//{ Driver Code Starts.
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
void push(struct Node** head_ref, int new_data) {
Node* new_node = new Node(new_data);
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main() {
long test;
cin >> test; //input testcases
while (test--) { //while testcases exist
struct Node* a = NULL;
long n, tmp;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> tmp;
push(&a, tmp);
}
Solution obj;
a = obj.mergeSort(a);
printList(a);
}
return 0;
}
// } Driver Code Ends