Search code examples
csortingbubble-sort

Bubble sort issue with ascending order


I have a program here that asks the user to enter up to 20 numbers. It then displays the numbers entered with duplicates removed and then uses a bubble sort to display them in ascending order with the duplicates removed. My issues is in the bubble sort. When it lists the nos in ascending order the last number always gets removed. Can someone help me by showing why it's doing that.

#include <stdio.h>    

/* This program asks the user to enter up to 20 numbers. It then displays the numbers entered, removes duplicates
 and then list the numbers with the duplicates removed in ascending order.
 */
int main (void)
{
    setbuf(stdout, NULL);

    int nums[20] , i , j, k, swap ;
    int count=0;

    {
        printf("Enter integers. (Negative -1 to stop):\n");
        for (i=0; i < 20; i++)
        {
            scanf("%d", &nums[i]);
            count = count +1;

            if(nums[i] == -1 ) // If user enters -1 stops the program from expecting anymore number inputs
                break;
        }
    }

    printf("The numbers you entered are:\n"); // outputs the numbers you entered one number per line
    for(i=0;i<count;++i)
    {
        printf("%d\n", nums[i]);
    }

    printf("\n Your numbers with the duplicate numbers removed:\n ");
    // for loop for removing the duplicate numbers that the user enters.
    for(i=0;i<count;i++)
    {
        for(j=i+1;j<count;)
        {
            if(nums[j]==nums[i])
            {
                for(k=j;k<count-1;++k)
                {
                    nums[k]=nums[k+1];
                }
                count--;
            }
            else
            {
                j++;
            }
        }
    }

    for(i=0;i<count;i++) // outputs the numbers you entered with the duplicates removed one number per line
        printf("%d\n ",nums[i]);

    // start of the bubble sort for listing the numbers in ascending order. Can replace ">" with "<" to list in descending order
    for(i=0; i<(k-1); i++)
    {
        for(j=0; j < k - i; j++)
        {
            if (nums[j] > nums[j+1])
            {
                swap = nums[j];
                nums[j] =nums[j+1];
                nums[j+1] = swap;
            }
        }
    }
    printf("\nYour numbers sorted in to ascending order with the duplicates removed:\n");

    for(j=0;j<i;j++) // outputs the numbers in ascending order. One number per line
        printf("%d\n ",nums[j]);


    return 0;
}

Solution

  • There are two issues.

    Keep in mind that after removing the duplicates, you have the total number of entries in your variable count.

    So it'd be easier to do

    for(i=0; i < count - 1; i++)
    {
        for(j=0; j < count - 1 - i; j++)
        {
            .....
    

    Notice the -1 in the second loop. This prevents the iteration from going out of bounds, since you are using j+1

    The second error is simply in your printing loop.

    Since you already have the amount of numbers to print stored in count, change

    for(j = 0; j < i; j++)
    

    to

    for(j = 0; j < count; j++)