#include <stdio.h>
#define SIZE 10
int main()
{
int a[ SIZE ];
int i,n,pass,hold;
printf("enter the number of elements in an array:");
scanf("%d",&n);
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++ )
printf( "%d %d ", a[ i ], hold );
if ( a[ i ] > a[ i + 1 ] ) {
hold = a[ i ];
a[ i ] = a[ i + 1 ];
a[ i + 1 ] = hold;
}
printf( "\nData items in ascending order\n" );
for ( i = 0; i <= SIZE - 1; i++ )
printf( "%4d", a[ i ] );
printf( "\n" );
return 0;
}
the above program is for bubble sorting....
but it is not running properly....when i enter number of elements in an array:7
and enter array elements:5 10 15 14 16 18 20 ....it prints...some weird output of array in ascending order :1280315..1280313..etc..and then prints data items in ascending order after bubble sorting is:5 10 15 16 18 20...
whats wrong with the code?plz make some necessatu changes and plz explain th code if u can to make the program run successfully...im really confused now...????could u plz explain the functioning of for loops... here,,,???im confused now how is that working ..why have we taken i=size -2?in the for loop?plz explain the for loops ?
and plz make my program run correctly....its not showing any error but not running properly......