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

1 thought on “Bubble Sort Program In C Using Function”

  1. Decent website with easy explanation

    Reply

Leave a Comment