Thursday, 20 March 2014

BUBBLE SORT USING CALL BY REFERENCE

/*………This program puts values into an array, sorts the values into ascending order, and prints the resulting array….. */
#include <stdio.h>
#define SIZE 10
void bubbleSort( int *, const int );                              /*…Prototype…*/
/*…Main Function Starts…*/
void main()
{
/*….Initialization and Declaration…….*/
int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };                    
int i;
printf( “Data items in original order\n” );
for ( i = 0; i < SIZE; i++ )                                                      /*..CHecking Condition……*/
printf( “%4d”, a[ i ] );
bubbleSort( a, SIZE );                                                           /* sort the array */
printf( “\nData items in ascending order\n” );
for ( i = 0; i < SIZE; i++ )                                                     /*..CHecking Condition……*/
 printf( “%4d”, a[ i ] ); 
printf( “\n” );
getch();
}
/*….Main Function Ends…*/
void bubbleSort( int *array, const int size )                       /*..Function Definition…*/
{
void swap( int *, int * );
int pass, j; 
for ( pass = 0; pass < size – 1; pass++ )
for ( j = 0; j < size – 1; j++ )
if ( array[ j ] > array[ j + 1 ] )
swap( &array[ j ], &array[ j + 1 ] );
}
void swap( int *element1Ptr, int *element2Ptr )                 /*…Function Definition………*./
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}

Image

No comments:

Post a Comment