Merge sort for single linked lists
belongs to collection: Data Structure programs using C and C++ (Sorting Programs)
All Answers
total answers (1)
belongs to collection: Data Structure programs using C and C++ (Sorting Programs)
total answers (1)
Algorithm for merge sort
1. Split the list into two halves
We use Floyd’s tortoise & hare algorithm for partitioning the linked list into two halves. We declare a slow pointer and a fast pointer. Slow pointer moves one step ahead where the fast pointer moves two step at a time. Thus when the fast pointer reaches the end of the list, the slow pointer is at the midway.
2. Call recursive function mergeSort() two sort the individual lists
Let split1 & split2 points to the two individual list after partitioning.
We call mergeSort() recursively two sort this two list.
3. Merge two sorted list
Finally merge two sorted list into another sorted list.
We can do it by comparing elements of two list.
C++ implementation for Merge sort for single linked lists
Output