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

Bubble Sort In C Using Pointers Program

Bubble Sort In C Using Pointers. If you are looking for a bubble sort program in C with pointers 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 pointers.

Bubble Sort in C Using Pointers

Bubble Sort in C – Table of Contents

What is Bubble Sort?

Bubble Sort in C 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

C Programming Tutorials

  1. Bubble Sort Program in C Using Recursion
  2. Bubble Sort Program in C Using Array
  3. Bubble Sort Program in C Using Function
  4. Stack Push Pop Program in C Using Arrays
  5. Factorial Program in C Using Pointers
  6. Factorial Program in C Using While Loop
  7. C Program For Factorial Using For Loop
  8. Factorial Program in C Using Recursion Function

Bubble Sort in C Using Pointers

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

Bubble Sort Program in C Using Pointers – 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 Pointers with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING POINTERS - BUBBLESORT.C */

#include<stdio.h>

int *a[100],i,j,item;

void main()

{

   void sort(),display(); //function declarations

   int i;

   clrscr();

   printf("\n Enter the number of elements in the first array\n");

   scanf("%d",&item); //reding elements from user

   printf("\n Enter %d numbers\n",item);

   for(i=0;i<item;++i)

   scanf("%d",&a[i]);
  
   sort(); //calling sort function to perform bubble sort

   display(); //calling display function to display elements

}

 void sort() //sort function to sort elements

 {

   int swap=1,*temp;

   for(i=0;i<item && swap==1;++i)

   {

    swap=0;

    for(j=0;j<item-(i+1);++j)

    if (a[j]>a[j+1])
    {

     temp=a[j];

    a[j]=a[j+1];

    a[j+1]=temp;

    swap=1;
   }

  }

 }


 void display() //display function

 {

  printf("\n Sorted elements are:\n");

  for(i=0;i<item;++i)

  printf("%d\n",a[i]);

  getch();

 }

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using pointers, 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

Bubble Sort Program In C Using Recursion

Bubble Sort In C Using Recursion – If you are looking for a bubble sort program in C with recursion 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 recursion.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

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

C Programming Tutorials

  1. Bubble Sort Program in C Using Array
  2. Bubble Sort Program in C Using Function
  3. Stack Push Pop Program in C Using Arrays
  4. Factorial Program in C Using Pointers
  5. Factorial Program in C Using While Loop
  6. C Program For Factorial Using For Loop
  7. Factorial Program in C Using Recursion Function

Bubble Sort in C Using Recursion

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

Bubble Sort Program in C Using Recursion – 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 Function with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING RECURSION - BUBBLESORT.C */

#include <stdio.h>
  #include <stdlib.h>

  /* sorts the given numbers in ascending order */
  void bubbleSort(int *data, int n) {
        int i, temp;

        if (n > 0) {
                for (i = 1; i < n; i++) {
                        if (data[i - 1] > data[i]) {
                                temp = data[i];
                                data[i] = data[i - 1];
                                data[i - 1] = temp;
                        }
                }

                bubbleSort(data, n - 1);
        }

        return;
  }

  int main() {
        int i, n, *data;

        /* get the number of inputs from the user */
        printf("Enter the number of inputs:");
        scanf("%d", &n);

        /* dynamically allocate memory to store i/p values */
        data = (int *) malloc(sizeof(int) * n);

        /* get the input data from the user */
        for (i = 0; i < n; i++) {
                printf("data[%d]: ", i);
                scanf("%d", &data[i]);
        }
        /* sorts the given numbers */

        bubbleSort(data, n);




        /* print the sorted numbers */

        printf("Data After Bubble Sort:\n");

        for (i = 0; i < n; i++) {

                printf("%d ", data[i]);

        }

        printf("\n");




        return 0;

  }

C PROGRAM FOR BUBBLE SORT – OUTPUT

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

 

C PROGRAMMING EXAMPLES

Bubble Sort Program In C Using Function

Bubble Sort Program In C Using Function. If you are looking for a bubble sort program in C with function 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 tutorial to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using function.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

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

C Programming Tutorials

  1. Bubble Sort Program in C Using Array
  2. Stack Push Pop Program in C Using Arrays
  3. Factorial Program in C Using Pointers
  4. Factorial Program in C Using While Loop
  5. C Program For Factorial Using For Loop
  6. Factorial Program in C Using Recursion Function

Bubble Sort in C with Function

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

Bubble Sort Program in C Using Function – 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 Function with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING FUNCTION - BUBBLESORT.C */

#include<stdio.h>
#include<stdlib.h>
void bubblesort(int a[],int size);
void main()
{
  int a[50],n,i;
  printf("\n Enter the size of the array");
  scanf("%d",&n);
  if(n>50)
  {
    printf("\n error");
    exit(0);
  }

 printf("\n Enter the array elements: \n");

 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  bubblesort(a,n);
  printf("\n The sorted array is\n");
  for(i=0;i<n;i++)
  printf("%d\t",a[i]);
}
void bubblesort(int a[],int size)
{
  int temp,i,j;
  for(i=0;i<size;i++)
  {
   for(j=0;j<size-1;j++)
   {
    if(a[j]>a[j+1])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
  }
 }
}

C PROGRAM FOR BUBBLE SORT – OUTPUT

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

Output:

Enter the size of the array: 5

Enter the array elements: 29541

The sorted array is 12459

C PROGRAMMING EXAMPLES

Bubble Sort Program In C Using Array With Example

Bubble Sort Program In C Using Array – If you are looking for a bubble sort program in C with array 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 an C program for bubble sort using array.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

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.

Bubble Sort Program in C
Bubble Sort Program in C

C Programming Tutorials

  1. Stack Push Pop Program in C Using Arrays
  2. Factorial Program in C Using Pointers
  3. Factorial Program in C Using While Loop
  4. C Program For Factorial Using For Loop
  5. Factorial Program in C Using Recursion Function
  6. C Program For Palindrome String
  7. C Program For Palindrome Numbers
  8. C Program To Reverse a String with Using Function
  9. C Program To Reverse a String without Using Function
  10. C Program To Reverse a String Using Recursion

Bubble Sort in C with Array

Learn how to write a  bubble sort in c using array. Writing a c program for bubble sort can be done using various techniques like array, pointers, recursion, function but here in this program, we show how to write a bubble sort program in c using array in a proper way.

Bubble Sort Program in C – 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 program on bubble sort using this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING ARRAY - BUBBLESORT.C */

#include<stdio.h>

//program execution starts from here. 

int main()

{
    int a[50],n,i,j,temp; //Variable declaration

    printf("Enter the size of array: ");

    scanf("%d",&n); //Taking size of the array from user

    printf("Enter the array elements: "); 

    for(i=0;i<n;++i)

        scanf("%d",&a[i]); //Taking array elements from user
   
    for(i=1;i<n;++i)

        for(j=0;j<(n-i);++j)

            if(a[j]>a[j+1]) //Bubble sort in c using array logic

            {

                temp=a[j];

                a[j]=a[j+1];

                a[j+1]=temp;

            }            

    printf("\nArray after sorting: ");

    for(i=0;i<n;++i)

        printf("%d ",a[i]); //Printing elements after bubble sort

    return 0;

}

C Program for Bubble Sort – Output

After you compile and run the above bubble sort program in c using array, your C compiler asks you to enter array size and array elements for bubble sort. After you enter elements, the program will be executed and give output like below expected output using bubble sort functionality.

Output:

Enter the size of array: 5
Enter the array elements: 29541

Array after sorting: 12459

C PROGRAMMING EXAMPLES