0

I actually try to get the hang of using bubble sort. So i did a little practice in a online code space with a 10 int array, but somehow it doesn´t work competely right yet.

#include <stdio.h>

int main()
{

  int values[10] = {3, 7, 4, 9, 11, 2, 4, 12, 5, 39};
  int n = 10;

  for (int i = 0; i < n - 1; i++)
  {
    for (int c = 0; c < n - i - 1; c++)
    {
        if (values[i] > values[i + 1])
        {
            int swap = values[i];
            values[i] = values[i + 1];
            values[i + 1] = swap;

        }

    }
  }

  for (int k = 0; k < n; k++)
        {
            printf("%d\n", values[k]);  
        }

}

The Output is :

3 4 7 9 2 4 11 5 12 39

So for some reason not all numbers seem to bubble up correctly, but i just can´t nail the issue here...

1

Hmmm.... Do you think that maybe the code inside the inner loop (the if statement and its code block) should be based on c instead of i?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • You shooled me hard there... thank you :) Oct 20 '17 at 7:06
  • Hey, it's always the simple mistakes that hide in plain sight. ;-) Otherwise, nice job!
    – Cliff B
    Oct 20 '17 at 7:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .