Bubble Sort In C Using Liked List Program

Bubble Sort In C Using Linked List. If you are looking for a bubble sort program in C with linked list example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming example to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using linked list.

Bubble Sort in C Using Linked List

Bubble Sort in C – Table of Contents

What is Bubble Sort?

Bubble Sort in C – Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

Bubble Sort Program in C
Bubble Sort Program in C

Bubble Sort in C Programming Tutorials

  1. Bubble Sort in C Using Pointers
  2. Bubble Sort Program in C Using Recursion
  3. Bubble Sort Program in C Using Array
  4. Bubble Sort Program in C Using Function

Bubble Sort in C Using Linked List

Learn how to write a  bubble sort in c using linked list. Writing a Bubble Sort C Program can be done using various techniques like an array, pointersrecursionfunction but here in this program, we show how to write a bubble sort program in c using linked list in a proper way.

Bubble Sort Program in C Using Linked List – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using linked list with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING LINKED LIST - BUBBLESORT.C */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
struct lnode {
 int data;
 struct lnode *next;
} *head, *visit;
 
/* adding a new entry to the linked list */
void llist_add(struct lnode **q, int num);
/* preforming a bubble sort on the linked list */
void llist_bubble_sort(void);
/* printing the entire linked list */
void llist_print(void);
 
int main(void) 
{
 
/* linked list */
 struct lnode *newnode = NULL;
 int i = 0,num,a[20]; /* variable declaration */
 
 printf("\n\n\t BUBBLE SORT IN C USING LINKED LIST\n"); 
 printf("\nEnter the number of elements : ");
 scanf("%d",&num); //Reading the number of elements from user
 /* Enter some random elements into the linked list */
 printf("\nEnter the elements to be sorted : \n");
 for(i = 0; i < num; i++) 
{
 scanf("%d",&a[i]);
 llist_add(&newnode, a[i]);
 }
 
 head = newnode;
 printf("\nList of input elements :\n");
 
 llist_print();
  
 printf("\n\nSorted List of elements by bubble sort :\n");
  
 llist_bubble_sort();
 llist_print();
 getch();
 return 0;
}
 
/* This logic will add a node at the end of a linked list */
void llist_add(struct lnode **q, int num) {
 struct lnode *tmp;
 
 tmp = *q;
 
 /* if the list is empty, create first node */
 if(*q == NULL) {
 *q = malloc(sizeof(struct lnode));
 tmp = *q;
 } else {
 /* go to last node */
 while(tmp->next != NULL)
 tmp = tmp->next;
 
 /* add node at the end */
 tmp->next = malloc(sizeof(struct lnode));
 tmp = tmp->next;
 }
 
 /* assign data to the last node */
 tmp->data = num;
 tmp->next = NULL;
}
 
/* print the entire linked list */
void llist_print(void) {
 visit = head;
 
 while(visit != NULL) {
 printf("%d\t", visit->data);
 visit = visit->next;
 }
 printf("\n");
}
 
/* preforming a bubble sort in c using linked list */
void llist_bubble_sort(void) {
 struct lnode *a = NULL;
 struct lnode *b = NULL;
 struct lnode *c = NULL;
 struct lnode *e = NULL;
 struct lnode *tmp = NULL;
 
 /*
 // the `c' node precedes the `a' and `e' node
 // pointing up the node to which the comparisons
 // are being made.
 */
 while(e != head->next) {
 c = a = head;
 b = a->next;
 while(a != e) {
 if(a->data > b->data) {
 if(a == head) {
 tmp = b -> next;
 b->next = a;
 a->next = tmp;
 head = b;
 c = b;
 } else {
 tmp = b->next;
 b->next = a;
 a->next = tmp;
 c->next = b;
 c = b;
 }
 } else {
 c = a;
 a = a->next;
 }
 b = a->next;
 if(b == e)
 e = a;
 }
 }
}

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using linked list, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output.

C PROGRAMMING EXAMPLES

Leave a Comment