Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions Cpp/MergeSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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