Search code examples
objective-ccbubble-sort

Bubble sort Descending and Ascending in C won't sort


I'm giving a user choices to whether sort the elements in ascending or descending order. I know my code can sorts the elements right but somewhere in main I think I'm making mistake in calling my function to print the ascending/descending element in their proper order. Or do I have to have another if statement like I have in the bubble_sort function? I need to make it so the Main function prints the final results to the user. Here's the output I'm getting: Enter number of elements 3 Enter 3 integers 43 7 90 Enter sort order Please enter A for ascending or D for descending order d Sorted list in descending order: 43 7 90

#include <stdio.h>

void bubble_sort(long [], char n);

int main()
{
    long array[100], n, c;

    printf("Enter number of elements\n");
    scanf("%ld", &n);

    printf("Enter %ld integers\n", n);
    for (c = 0; c < n; c++)
        scanf("%ld", &array[c]);

    printf("Enter sort order\n");
    fflush(stdin);
    printf("Please enter A for ascending or D for descending order\n");
    scanf("%ld", &n);
    bubble_sort(array, n);
    printf("Sorted list in descending order:\n");
    for ( c = 0 ; c < n ; c++ )
    {
        printf("%ld\n", array[c]);
    }
    fflush(stdin);
    getchar();
    return 0;
}

void bubble_sort(long list[], char n)
{
    long c, d, temp;

    if(n=='a' || n=='A')
    {
        for (c = 0 ; c < ( n - 1 ); c++)
        {
            for (d = 0 ; d < n - c - 1; d++)
            {
                if (list[d] > list[d+1])
                {
                    temp = list[d];
                    list[d] = list[d+1];
                    list[d+1] = temp;
                }
            }
        }
    }
    if(n=='d' || n=='D')
    {
        long c, d, temp;

        for (c = 0 ; c < ( n - 1 ); c++)
        {
            for (d = 0 ; d > n - c - 1; d++)
            {
                if (list[d] < list[d+1])
                {/* Swapping */
                    temp = list[d];
                    list[d] = list[d+1];
                    list[d+1] = temp;
                }
            }
        }
    }
}

EDIT: Here I added a swap function just so the ascending/descending logic is more efficient. But I seem to mixed up use of the variables which I think is a big problem. Would anyone point out and help me understand where and why I'd need to use those variables? Thanks much!

#include <stdio.h>

void bubble_sort(int list[], int n, char c);
void swap(int x, int y, int array[]);
int main()
{
    int array[100], j, i;
    char c;
    printf("Enter number of elements\n");
    scanf("%d", &j);

    printf("Enter %d integers\n", j);
    for (i = 0; i < j; i++)
        scanf("%d", &array[i]);

    printf("Please enter A for ascending or D for descending order\n");
    scanf("%s", &c);

    bubble_sort(array, j, i);
    printf("Sorted list in descending order:\n");
    for (i = 0 ; i < j ; i++ )
    {
        printf("%d\n", array[i]);
    }
    getchar();
    return 0;
}

void bubble_sort(int list[], int n, char c)
{
    int i, j;

    if(c=='a' || c=='A'){
        for (i = 0; i < (n - 1); i++){
            for (j = 0; j < (n - i) - 1; j++){
                if (list[i] > list[j])
                {
                    swap(i, j, list);                }
            }
        }
    }
    if(c=='d' || c=='D') {
        for (i = 0 ; i < ( n - 1 ); i++) {
            for (j = 0 ; j > (n - i) - 1; j++) {
                if (list[i] < list[j])
                {
                    swap(i, j, list);
                }
            }
        }
    }
}

void swap(int x, int y, int array[])
{
    int hold; //temp hold a number

    hold = array[x];
    array[x] = array[y];
    array[y] = hold;
}

Solution

  • In this statements

    printf("Please enter A for ascending or D for descending order\n");
    scanf("%ld", &n);
    

    you are overwritting the value stored in n that before these statements denoted the number of the elements in the array. You should declare one more variable of type char and use it for this code snippet.

    Also the sort function should be declared as

    void bubble_sort(long list[], int n, char c );
    

    where n is the array size and c is either 'A' or 'D'

    EDIT: Your new code contains many typos. Try the following

    #include <stdio.h>
    
    void swap( int x, int y, int array[] )
    {
        int hold; //temp hold a number
    
        hold = array[x];
        array[x] = array[y];
        array[y] = hold;
    }
    
    void bubble_sort( int list[], int n, char c )
    {
        int i, j;
    
        if ( c == 'a' || c == 'A' )
        {
            for ( i = 0; i < n - 1; i++ )
            {
                for ( j = 0; j < n - i - 1; j++ )
                {
                    if ( list[j] > list[j+1] )
                    {
                        swap( j, j + 1, list);                
                    }
                }
            }
        }
    
        if ( c=='d' || c=='D' ) 
        {
            for ( i = 0 ; i < n - 1; i++ ) 
            {
                for ( j = 0 ; j < n - i - 1; j++ ) 
                {
                    if ( list[j] < list[j+1] )
                    {
                        swap( j, j + 1, list);
                    }
                }
            }
        }
    }
    
    int main(void) 
    {
        int array[100], j, i;
        char c;
    
        printf("Enter number of elements: ");
        scanf( "%d", &j);
    
        printf( "Enter %d integers\n", j );
        for ( i = 0; i < j; i++ ) scanf( "%d", &array[i] );
    
        printf("Please enter A for ascending or D for descending order: ");
        scanf( " %c", &c );
    
        printf( "%c\n", c );
    
        bubble_sort( array, j, c );
    
        printf( "Sorted list in the selected order:\n" );
        for ( i = 0; i < j; i++ )
        {
            printf( "%d ", array[i] );
        }
        puts( "" );
    
        return 0;
    }