
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Merge Sort Algorithm on Linked List in C++
The merge sort technique is based on the divide-and-conquer technique. We divide the whole data set into smaller parts and merge them into a larger piece in sorted order. It is also very effective for worst cases because this algorithm has lower time complexity for worst cases too.
A linked list can be sorted using merge sort very efficiently. For the linked list, the merging task is very simple. We can simply update the links to merge them. In this article, we have an unsorted linked list. Our task is to sort the given unsorted list using the merge sort technique in C++.
Example
The following example sorts the given unsorted list using merge sort:
Input: Linked List: 4 -> 2 -> 7 -> 1 -> 5 -> 3 Output: List after sorting: 1 -> 2 -> 3 -> 4 -> 5 -> 7
Steps to Implement Merge Sort on Linked List
The steps to implement the merge sort on the linked list are as follows:
- First, we check if the linked list is empty or has only a single node. If it has a single node, then we return it, as there is no need for sorting.
- Then the splitList() function finds the middle node of the list and splits the list into two halves using the fast and slow pointers.
- The slow pointer moves one node at a time while the fast pointer moves two nodes at a time. So, when the fast node is at the end of the list, the slow pointer is at the middle node of the list.
- After splitting the list in two halves, we sort both halves by recursively calling the mergeSort() function for each half of the list.
- After sorting, we use the mergeList() function to merge both sorted lists into a single linked list. We compare elements from each node. The smaller element is stored in the newhead. Similarly, we find the next smaller element among both lists.
- The mergeList() function is recursively called until both the sorted lists are merged in a sorted way.
- At the end, the sorted linked list is displayed using the display() function.
C++ Program to Implement Merge Sort on Linked List
We have implemented the above steps to apply merge sort on a linked list in C++:
#include <iostream> using namespace std; class node { public: int data; node* next; }; void display(node* start) { node* p = start; while (p != NULL) { cout << p->data << " "; p = p->next; } cout << endl; } node* getNode(int d) { node* temp = new node; temp->data = d; temp->next = NULL; return temp; } // Merging both sorted linked lists node* mergeList(node* ll1, node* ll2) { node* newhead = NULL; // Base cases if (ll1 == NULL) return ll2; if (ll2 == NULL) return ll1; // Creating sorted merged list by comparing node values if (ll1->data <= ll2->data) { newhead = ll1; newhead->next = mergeList(ll1->next, ll2); } else { newhead = ll2; newhead->next = mergeList(ll1, ll2->next); } return newhead; } // Splitting the linked list into two halves void splitList(node* start, node** ll1, node** ll2) { node* slow = start; node* fast = start->next; // Moving fast two steps and slow one step while (fast != NULL) { fast = fast->next; if (fast != NULL) { slow = slow->next; fast = fast->next; } } // Split the list into two halves *ll1 = start; *ll2 = slow->next; slow->next = NULL; } void mergeSort(node** start) { node* head = *start; node* ll1, *ll2; if (head == NULL || head->next == NULL) { return; } splitList(head, &ll1, &ll2); // Recursively sort each half mergeSort(&ll1); mergeSort(&ll2); // Merge the sorted halves *start = mergeList(ll1, ll2); } int main() { int values[] = {4, 2, 7, 1, 5, 3}; int n = sizeof(values) / sizeof(values[0]); node* head = getNode(values[0]); node* temp = head; for (int i = 1; i < n; ++i) { temp->next = getNode(values[i]); temp = temp->next; } cout << "Before sorting:\n"; display(head); mergeSort(&head); cout << "After sorting:\n"; display(head); return 0; }
The output of the above code is:
Before sorting: 4 2 7 1 5 3 After sorting: 1 2 3 4 5 7
Complexity of Merge Sort Algorithm on Linked List
- Time Complexity: The time complexity of implementing merge sort on a linked list is O(n log n).
- Space Complexity: The space complexity of implementing merge sort on a linked list is O(log n).