C Program to Implement Merge Sort Algorithm on Linked List

«
»
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.
15. {
17.     struct node* a;
18.     struct node* b;
20.     {
21.         return;
22.     }
24.     mergesort(&a);
25.     mergesort(&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;
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. }

3 5 10 15 20 26775

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 