Hey, I had a quick question. I'm supposed to modify the following code to make the bubble sort make less comparisons on each pass( eight on the second pass, seven on the third, etc.). What would you folks suggest I do?
-----
#include #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
" );
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 ];
a[ i ] = a[ i + 1 ];
a[ i + 1 ] = hold;
}
printf( "
Data items in ascending order
" );
for ( i = 0; i <= SIZE - 1; i++ )
printf( "%4d", a[ i ] );
printf( "
" );
return 0;
}
Comments
[code]
for ( pass = 0; pass <= SIZE - 1; pass++ )
for ( i = pass; i < SIZE; i++ )
[/code]