Bubble sort C program

It’s a bubble sort C program that inputs numbers from the user and stores these numbers using an array, then using bubble sort algorithm to arrange them in ascending order, and print result on the screen.

To understand this program, it’s better you should know the following topics:

Program (1): to demonstrate bubble sort in C programming.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,t,n;
printf("Enter the number of elements(upto 100) to be entered in the list : ",n);
scanf("%d",&n);
//accepting values into array
for(i=0;i<n;i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&a[i]);
}
//Sorting process begin
for(i=0;i<n;i++)
{
    for(j=0;j<n-i-1;j++)
    {
    if(a[j]>a[j+1])
    {
    //swapping procedure
    t=a[j];
    a[j]=a[j+1];
    a[j+1]=t;
    }
    }
    }
    //Printing an array a elements after sorting
    printf("\n\nAfter sorting list contain\n");
    for(i=0;i<n;i++)
    {
    printf("%d\t",a[i]);
    }
getch();
}

Output (1)

Enter the number of elements(upto 100) to be entered in the list : 4
Enter 1 element : 5
Enter 2 element : 2
Enter 3 element : 10
Enter 4 element : 1


After sorting list contain
1 2 5 10

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

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