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

Leave a Reply

Your email address will not be published. Required fields are marked *