There are a plethora of sorting algorithms present for arrays like the bubble sort, insertion sort, selection sort, heap sort, merge sort, quick sort, and many more. But in the case of Linked Lists, the implementation of all the sorting algorithms is difficult. Heapsort is totally impossible for sorting the linked list.
In this article, we will discuss how to apply the Merge Sort algorithm to sort the given linked list in ascending order.
Given a singly linked list containing n number of nodes, sort the linked list in the ascending order using the merge sort sorting algorithm.
Inputs:
- 11-> 2 -> 4 -> 1 -> 9-> null
- 1 -> null
Outputs:
- 1 -> 2 -> 4 -> 9 -> 11 -> null
- 1 -> null
Merge Sort Algorithm for Array’s data structure
- Check for the condition start<end.
- First of all, find the middle element of the array. Middle element is equal to the(start+(end-start)/2).
- Recursively call the array for 0(start) to mid element(end) and mid+1(start) to length-1 of the array until start < end .
- Call MergeSort (start ,mid ,end).
- In MergeSort Function, sort the two halves for the array.
The above algorithm is not suitable for the Linked List sorting because, in Linked List, we cannot access a particular node of the linked list in O(1). This is because of the non-contiguous memory allocation of the Linked List.
On the other hand, arrays have contiguous memory allocation. We can easily find the middle element but in the case of the linked list, we have to use another algorithm i.e the Tortoise-Hare Algorithm.
TORTOISE-HARE Algorithm
- Iterate the Linked List using two pointers/references, one pointer is slow and the other pointer/reference is fast.
- Update the slow pointer/reference by one node and the fast pointer/reference by two nodes.
- Return the slow pointer/reference which is the middle node of the linked list.
The rest of the steps to perform the merge sort in the linked list are the same with very few variations.
Approach
- Find the middle element of the Linked List using the TORTOISE-HARE algorithm.
- Divide the element into two halves head and middle.next position of the linked list.
- Recursively Divide the generated halves into another half till head or head.next becomes null.
- Merge the two halves by sorting each node’s data.
JAVA
Output:
Python
Output:
C++
Output:
Complexity
- Time Complexity : The Time complexity of this approach is O(nlog(n).
- Space Complexity : The Space Complexity of this approach is O(1).
Conclusion
Merge Sort for Linked Lists is one of the most interesting problems of Data structure. In this article, we have discussed an approach to sort a linked list using Merge Sort. Sorting of linked lists is a little bit difficult due to the non-contiguous memory location of the nodes available in the linked list. But with little variation of the naive merge sort algorithm, we can easily sort the linked list desired order.
We hope this article will help you to understand the problem statement and you will be able to solve any other variation of this problem.
Happy Coding!
People are also reading:
- Rearrange an array in maximum minimum form
- Print a given matrix in spiral form
- Sort binary array in linear time
- What is Structured Programming?
- Construct a tree from Inorder and Level order traversals
- Write a Program to convert given inches into equivalent yard, and feet
- DSA Binary Search Tree
- Find minimum jumps required to reach the destination
- DSA: Binary Heap Tree
- Fibonacci Series
Leave a Comment on this Post