Below i have implemented a bubble sort function but i need help in making the bubble sort having a trace function. I just want to print out each pass of the sort until it is finally sorted.
Code:
#include <stdio.h>
#define SIZE 10
int main()
{
int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int i, pass, hold;
printf( "Data items in original order\n" );
for ( i = 0; i <= SIZE - 1; i++ )
printf( "%4d", a[ i ] );
for ( pass = 1; pass <= SIZE - 1; pass++ )
for( i = 0; i <= SIZE - 2; i++ )
if ( a[ i ] > a[ i + 1 ] ) {
hold = a[ i ]; /* store the value */
a[ i ] = a[ i + 1 ]; /* assign a[i] the a[i+1] value */
a[ i + 1 ] = hold; /* assign a[i+1] the hold value */
}
printf( "\nData items in ascending order\n" );
for ( i = 0; i <= SIZE - 1; i++ )
printf( "%4d", a[ i ] );
printf( "\n" );
return 0;
}