Bubble Sorting Program and Tracing Video:
Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order.
Pass 1
0 1 2 3 4
---------------------------
7 5 8 9 2
7>5T
5 7 8 9 2
7>8F
8>9F
9>2T
5 7 8 2 9
---------------------------------------
Pass 2
5 7 8 2 9
5>7F
7>8F
8>2T
5 7 2 8 9
---------------------------------------
Pass 3
5>7F
7>2T
5 2 7 8 9
Pass 4
--------------------------------
5>2T
2 5 7 8 9
#include <stdio.h>
int main( )
{
int n,a[ 50 ],i,j,temp;
printf("\nEnter the size of array ");
scanf("%d ",&n);
printf("\nEnter the elements of array ");
for( i=0;i<n;i++ )
{
scanf(" %d ",&a[ i ]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[ j ] > a[ j+1 ])
{
temp=a[ j ];
a[ j ]=a[ j+1 ];
a[ j+1 ]=temp;
}
}
}
printf("\nElements of array after sorting are \n ");
for(i=0;i<n;i++)
{
printf(" %3d ",a[ i ]);
}
return 0;
}
Comments
Post a Comment