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.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* sortedmerge(struct node* a, struct node* b);
void frontbacksplit(struct node* source, struct node** frontRef, struct node** backRef);
void mergesort(struct node** headRef)
{
struct node* head = *headRef;
struct node* a;
struct node* b;
if ((head == NULL) || (head -> next == NULL))
{
return;
}
frontbacksplit(head, &a, &b);
mergesort(&a);
mergesort(&b);
*headRef = sortedmerge(a, b);
}
struct node* sortedmerge(struct node* a, struct node* b)
{
struct node* result = NULL;
if (a == NULL)
return(b);
else if (b == NULL)
return(a);
if ( a-> data <= b -> data)
{
result = a;
result -> next = sortedmerge(a -> next, b);
}
else
{
result = b;
result -> next = sortedmerge(a, b -> next);
}
return(result);
}
void frontbacksplit(struct node* source, struct node** frontRef, struct node** backRef)
{
struct node* fast;
struct node* slow;
if (source==NULL || source->next==NULL)
{
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source -> next;
while (fast != NULL)
{
fast = fast -> next;
if (fast != NULL)
{
slow = slow -> next;
fast = fast -> next;
}
}
*frontRef = source;
*backRef = slow -> next;
slow -> next = NULL;
}
}
void printlist(struct node *node)
{
while(node != NULL)
{
printf("%d ", node -> data);
node = node -> next;
}
}
void push(struct node** head_ref, int new_data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node -> data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct node* a = NULL;
push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&a, 20);
push(&a, 3);
push(&a, 26775);
mergesort(&a);
printf("\n Sorted Linked List is: \n");
printlist(a);
return 0;
}
$ gcc linkedlistsort.c -o linkedlistsort $ ./linkedlistsort Sorted Linked List is: 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!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice BCA MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Apply for C Internship
- Buy C Books