Thread: My call by reference bubble sort code isn't displaying the correct output

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    25

    My call by reference bubble sort code isn't displaying the correct output

    The program compiles, however the output is incorrect

    Output:
    Code:
    Input size of array5
    Input elements3
    5
    4
    7
    8
    Enter 1 to sort in ascending order,
    Enter 2 to sort in descending order: 1
    
    Data items in ascending order
       0327673276716064169201606420320
    logout
    
    [Process completed]
    Code:
    /* This program puts values into an array, sorts the values into
     ascending order, and prints the resulting array. */
    #include <stdio.h>
    
    void sortascending( int * const array, const int size ); /* prototype */
    void sortdescending( int * const array, const int size );
    void swap( int *element1Ptr, int *element2Ptr ); /* prototype */
    
    int main( void )
    {
    	
    	int i; /* counter */
    	int order;
    	/* initialize array a */
    	int SIZE;
    	int a[ SIZE ];
    	printf("Input size of array");
    	scanf("%d", &SIZE);
    	printf("Input elements");
    	for (i=0; i<SIZE; i++) {
    		scanf("%d", &a[SIZE]);
    	}
    	
    	printf( "Enter 1 to sort in ascending order,\n" 
               "Enter 2 to sort in descending order: " );
    	scanf( "%d",  &order );
    	
    	/* sort array in ascending order; pass function ascending as an
         argument to specify ascending sorting order */
    	switch (order) {
    		case 1:
    			sortascending( a, SIZE);
    			printf( "\nData items in ascending order\n" );
    			/* loop through array a */
    			for ( i = 0; i < SIZE; i++ ) {
    				printf( "%4d", a[ i ] );   
    			} /* end for */
    			printf( "\n" );
    			break;
    		case 2:
    			sortdescending( a, SIZE);
    			printf( "\nData items in descending order\n" );
    			/* loop through array a */
    			for ( i = 0; i < SIZE; i++ ) {
    				printf( "%4d", a[ i ] );   
    			} /* end for */
    			printf( "\n" );
    			break;
    		default:
    			break;
    			return 0; /* indicates successful termination */
    	} /* end main */
    }
    
    /* sort an array of integers using bubble sort algorithm */
    void sortascending( int * const array, const int size )
    {
    	int pass; /* pass counter */
    	int j;    /* comparison counter */
    	
    	/* loop to control passes */
    	for ( pass = 0; pass < size - 1; pass++ ) {
    		
    		/* loop to control comparisons during each pass */
    		for ( j = 0; j < size - 1; j++ ) {
    			
    			/* swap adjacent elements if they are out of order */
    			if ( array[ j ] > array[ j + 1 ] ) {
    				swap( &array[ j ], &array[ j + 1 ] );
    			} /* end if */
    			
    		} /* end inner for */
    		
    	} /* end outer for */
    	
    } /* end function bubbleSort */
    
    /* sort an array of integers using bubble sort algorithm */
    void sortdescending( int * const array, const int size )
    {
    	int pass; /* pass counter */
    	int j;    /* comparison counter */
    	
    	/* loop to control passes */
    	for ( pass = 0; pass < size - 1; pass++ ) {
    		
    		/* loop to control comparisons during each pass */
    		for ( j = 0; j < size - 1; j++ ) {
    			
    			/* swap adjacent elements if they are out of order */
    			if ( array[ j ] < array[ j + 1 ] ) {
    				swap( &array[ j ], &array[ j + 1 ] );
    			} /* end if */
    			
    		} 			
    	} 
    	
    }
    /* swap values at memory locations to which element1Ptr and
     element2Ptr point */
    void swap( int *element1Ptr, int *element2Ptr )
    {
    	int hold = *element1Ptr;
    	*element1Ptr = *element2Ptr;
    	*element2Ptr = hold;
    } /* end function swap */
    Last edited by dsured; 02-09-2011 at 07:31 PM. Reason: edit code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM
  4. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM