Skip to main content

Merge sort a linked list

Sorting a linked list is much more complex than sorting an array. Because you need to be wary of links at each step and update them.

The most convenient ways of sorting a linked list is insertion sort, where in you remove one node at a time from list and insert them to the sorted list in correct position.

But merge sorting a linked list uses another approach. It splits the list into two halves, sorts them recursively and then merges them maintaining ascending order of key values.

So the three basic parts of this approach are
  1. Splitting the list into two halves of equal length
  2. Sorting these halves
  3. Merging the halves
Step 2 is in fact not needed if the list has only one node. One node is sorted. Hence the merge sort algorithm is

  1. If the list has more than one node
  2. Splitting the list into two halves of equal length
  3. Sorting these halves recursively using steps 2 to 4
  4. Merging the halves 

 Split the list into halves

We have seen in this post how to find the mid point of a linked list. Let us use this approach.

We need to use two pointers say slow and fast. Slow moves one node at a time. And fast moves two nodes at a time. When fast has reached end of the list, slow has reached mid point. 

And also we should detach first half of the list from second half.

NODEPTR split_list(NODEPTR head)
{
    NODEPTR slow,fast;
    slow =head; 
    fast = head;
    if(head==NULL || head->next==NULL)     
       return head; /* if only one node is present, return*/
    NODEPTR temp;
    while(fast!=NULL)         
    {       
        fast = fast->next;        
        if(fast!=NULL){
           temp = slow;
           fast = fast->next;
           slow = slow->next;
        }           
     }     
     temp->next  = NULL;/*detach first half of list from second half*/
     return slow; /*this is the head of second half of list*/
}

 Merge sorted sublists

To merge the two sublists which are already sorted we need to compare nodes from two lists, insert the smaller one into the merged list and move to next node in that list. This process has to be repeated until all nodes of both lists are merged. If the two sublists are of unequal length then one list will have nodes remaining, which should be added to merged list

NODEPTR merge_sorted_lists(NODEPTR head1,NODEPTR head2)
{
 NODEPTR newlist = NULL;
 while(head1!=NULL && head2!=NULL )
 {
    if(head1->val <head2->val)
  {
  NODEPTR temp = head1;
  head1 = head1->next; 
  temp->next = NULL;
  newlist = append_node(newlist,temp);
  }
    else
  {
  NODEPTR temp = head2;
  head2 = head2->next; 
  temp->next = NULL;
  newlist = append_node(newlist,temp);
  }
 }
 while(head1!=NULL)
 {
  NODEPTR temp = head1;
  head1 = head1->next; 
  temp->next = NULL;
  newlist = append_node(newlist,temp);
  }
 while(head2!=NULL)
 {
  NODEPTR temp = head2;
  head2 = head2->next; 
  temp->next = NULL;
  newlist = append_node(newlist,temp);
 }
 return newlist;  
}  
 

Merge sort the list

 Let us use these two functions in the algorithm given earlier to merge sort the list.

NODEPTR merge_sort(NODEPTR head)
{
     if(head==NULL || head->next==NULL)
       return head; /* list has one node or is empty*/
     NODEPTR mid = split_list(head);
     head = merge_sort(head);/* sort first sublist*/
     mid = merge_sort(mid);/* sort second sublist*/
     head = merge_sorted_lists(head,mid);/* merge these*/
     return head;
}

You can download the entire program from here 

Comments

Popular posts from this blog

Introduction to AVL tree

AVL tree is a balanced binary search tree where the difference between heights of two sub trees is maximum 1. Why balanced tree A binary tree is good data structure because search operation here is of the order of O(logn). But this is true if the tree is balanced - which means the left and right subtrees are almost equal in height. If not balanced, search operation will take longer.  In worst case, if the tree has only one branch, then search is of the order O(n). Look at this example.  Here all nodes have only right children.  To search a value in this tree, we need upto 7 iterations, which is O(n). So this tree is very very inefficient. One way of making the tree efficient is to, balance the tree and make sure that height of two branches of each node are almost equal. Height of a node Height of a node is the distance between the node and its extreme child.  In the above a diagram, height of 37 is 3 and height of left child of 37 is 0 and right child of 37 is 2. Bal...

Reverse a singly linked list

One of the commonly used interview question is - how do you reverse a linked list? If you talk about a recursive function to print the list in reverse order, you are so wrong. The question is to reverse the nodes of list. Not print the nodes in reverse order. So how do you go about reversing the nodes. You need to take each node and link it to previous node. But a singly linked list does not have previous pointer. So if n1 is current node, n2 = n1->next, you should set     n2->next = NULL But doing this would cut off the list at n2. So the solution is recursion. That is to reverse n nodes  n1,n2,n3... of a list, reverse the sub list from n2,n3,n4.... link n2->next to n1 set n1->next to NULL The last step is necessary because, once we reverse the list, first node must become last node and should be pointing to NULL. But now the difficulty is regarding the head? Where is head and how do we set it? Once we reach end of list  viz n1->next ==NULL, th...

Balanced brackets

Have you observed something? When ever you are writing code using any IDE, if you write mismatched brackets, immediately an error is shown by IDE. So how does IDE  know if an expression is having balanced brackets? For that, the expression must have equal number of opening and closing brackets of matching types and also they must be in correct order. Let us look at some examples (a+b)*c+d*{e+(f*g)}   - balanced (p+q*[r+u )] - unbalanced (p+q+r+s) ) - unbalanced (m+n*[p+q]+{g+h}) - balanced So we do we write a program to check if an expression is having balanced brackets? We do need to make use of stack to store the brackets. The algorithm is as follows Scan a character - ch from the expression If the character is opening bracket, push it to stack If the character is closing bracket pop a character from stack If popped opening bracket and ch are not of same type ( ( and ) or [ and ] ) stop the function and return false Repeat steps 2 and 3 till all characters are scanned. On...