C Program to Sort Linked List using Merge Sort

This C program sorts an integer linked list using Merge Sort technique.

A linked list cannot be accessed randomly and because of this slow access time, sorting algorithms like quick sort cannot be applied to it. So we use merge sort for this purpose. It works on divide and conquer technique. Time complexity is O(nlogn).

Here is the source code of the C program to sort integers using Merge Sort on linked list technique. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node* next;
  8. };
  9.  
  10. struct node* sortedmerge(struct node* a, struct node* b);
  11. void frontbacksplit(struct node* source, struct node** frontRef, struct node** backRef);
  12.  
  13.  
  14. void mergesort(struct node** headRef)
  15. {
  16.     struct node* head = *headRef;
  17.     struct node* a;
  18.     struct node* b;
  19.     if ((head == NULL) || (head -> next == NULL))
  20.     {
  21.         return;
  22.     }
  23.     frontbacksplit(head, &a, &b);
  24.     mergesort(&a);
  25.     mergesort(&b);
  26.     *headRef = sortedmerge(a, b);
  27. }
  28.  
  29. struct node* sortedmerge(struct node* a, struct node* b)
  30. {
  31.     struct node* result = NULL;
  32.  
  33.     if (a == NULL)
  34.         return(b);
  35.     else if (b == NULL)
  36.         return(a);
  37.  
  38.     if ( a-> data <= b -> data)
  39.     {
  40.         result = a;
  41.         result -> next = sortedmerge(a -> next, b);
  42.     }
  43.     else
  44.     {
  45.         result = b;
  46.         result -> next = sortedmerge(a, b -> next);
  47.     }
  48.     return(result);
  49. }
  50.  
  51. void frontbacksplit(struct node* source, struct node** frontRef, struct node** backRef)
  52. {
  53.     struct node* fast;
  54.     struct node* slow;
  55.     if (source==NULL || source->next==NULL)
  56.     {
  57.         *frontRef = source;
  58.         *backRef = NULL;
  59.     }
  60.     else
  61.     {
  62.         slow = source;
  63.         fast = source -> next;
  64.         while (fast != NULL)
  65.         {
  66.             fast = fast -> next;
  67.             if (fast != NULL)
  68.             {
  69.                 slow = slow -> next;
  70.                 fast = fast -> next;
  71.             }
  72.     }
  73.  
  74.     *frontRef = source;
  75.     *backRef = slow -> next;
  76.     slow -> next = NULL;
  77.     }
  78. }
  79.  
  80. void printlist(struct node *node)
  81. {
  82.     while(node != NULL)
  83.     {
  84.         printf("%d ", node -> data);
  85.         node = node -> next;
  86.     }
  87. }
  88.  
  89. void push(struct node** head_ref, int new_data)
  90. {
  91.     struct node* new_node = (struct node*) malloc(sizeof(struct node));
  92.     new_node -> data  = new_data;
  93.     new_node->next = (*head_ref);
  94.     (*head_ref) = new_node;
  95. }
  96. int main()
  97. {
  98.     struct node* a = NULL;
  99.     push(&a, 15);
  100.     push(&a, 10);
  101.     push(&a, 5);
  102.     push(&a, 20);
  103.     push(&a, 3);
  104.     push(&a, 26775);
  105.     mergesort(&a);
  106.     printf("\n Sorted Linked List is: \n");
  107.     printlist(a);
  108.     return 0;
  109. }

$ gcc linkedlistsort.c -o linkedlistsort
$ ./linkedlistsort
 
Sorted Linked List is:
3 5 10 15 20 26775

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.