Bubble Sort program in C

Repetedly swapping of numbers to sort it into ascending or descending order is called bubble sort.

Step 1:

1  1  1  1
9  7  7  7
7  9  5  5
5  9  3
3  3  3  9

Step 2:

1  1  1  1
7  7  5  5
5  7  3
3  3  3  7
9  9  9  9

Step 3:

1  1  1
5  5  3
3  5
7  7  7
9  9  9

 

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,a[5];
clrscr();

printf(“enter the array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}

for(i=0;i<5;i++)  // repetedly sorting the array by concept of swapping
{
for(j=0;j<4;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf(“the sorted array is\n”); // printing the sorted array
for(i=0;i<5;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}

Output:

Leave a Reply