Search code examples
carrayssortingbubble-sort

Bubble Sorted array Error in C: Second integer is Always Zero


I have written a program to bubble sort an array of a length of no more than twenty integers whose contents are decided by the user. The problem I am experiencing is that when the sorted array is printed, the second integer in the array is always 0, causing the largest integer within the sorted array to not be printed. Here is my code below:

#include <stdio.h>

int main()
{
  int array[20];
  int n;
  int c;
  int d;
  float swap;
  printf("Please enter how many numbers you want to sort (up to 20):\n\t");
  scanf("%d", &n);
  if (n >= 2)
  {
    for (c = 0; c < n; c++)
    {
      printf("Please enter the next number:\n\t");
      scanf("%d", &array[c]);
    }
    for (c = 0; c < (n - 1); c++)
    {
      for (d = 0; d < (n - c); d++)
      {
        if (array[d] > array[d + 1])
        {
          swap = array[d];
          array[d] = array[d + 1];
          array[d + 1] = swap;
        }
      }
    }
    for (c = 0; c < n; c++)
    {
      printf("%d\n", array[c]);
    }
    printf("Here are the results in ascending order:\n");
  }
  else
  {
    printf("Since you have fewer than two numbers, they are already sorted!\n");
  }
  return 0;
}

Solution

  • for (d = 0; d < (n - c); d++)
    

    This is not right. Think about what will happen with six numbers (array elements zero through five inclusive).

    The first time through the swapping loop, n will be six and c will be zero. That means that d will take the values zero through five inclusive.

    Unfortunately, you are swapping indexes d and d + 1, the latter of which will be arbitrary data (index six) since you haven't filled it in.

    Since you're not seeing the last digit but are seeing zero instead, it's likely(a) that the element beyond the last one you're populating is set to zero. That would cause the swap code to swap it and your last element, resulting in what you saw:

    0   1   2   3   4   5   6   7   8   9 <-- indexes
    <--- you entered --->   remainder -->
    3   1   4   1   5   9   0   0   0   0 <-- data
                        \___/
                            \
                             these will swap
    

    What you should be using is:

    for (d = 0; d < n - c - 1; d++)
    

    That makes the difference between seeing(b):

    1 0 1 3 4 5
    

    and:

    1 1 3 4 5 9
    

    (a) Likely, but by no means guaranteed. Since array is a local variable, the value it takes for uninitialised elements will be arbitrary.

    If your code is sometimes working, it's because of this - you can get repeatable failure simply by ensuring the array elements are all initialised to zero:

    int array[20] = {0};
    

    Then, since it's reproducible, you can make the changes given in this answer to fix it.


    (b) As an aside, the reason the 0 is not bubbling all the way down to the bottom in this case is because it's being newly introduced in to the data during the first pass. That means there's one less pass than is needed to fully sort the data.