0

Code:

#include <stdio.h>

void testSort(int values[], int n);

int main(void)
{
    int hs[] = {5,3,2,1,4};
    printf("Unsorted: %i %i %i %i %i\n", hs[0], hs[1], hs[2], hs[3], hs[4]);
    testSort(hs, 5);

    printf("Sorted: %i %i %i %i %i\n", hs[0], hs[1], hs[2], hs[3], hs[4]);
}

void testSort(int values[], int n)
{
    for (int i = 0; i < n-1; i++)
    {
        int hold;
        int current = values[i];
        int next = values[i + 1];

        if (current > next)
        { 
            hold = current;
            current = next;
            next = hold;
        }
    }
    return;
}

I'm trying to do bubble sort and right now it goes through the array once, but my question is: Why isn't my hs[] updating after calling function? The second printf shows that it remained the same.

EDIT: As mentioned, turns out I was changing data but of the copies. For some reason I when I created the variables current/next I felt as if they were representing values[i]/values[i+1] but in reality I was just creating new variable and passing the value of values[0] which is 5 and assigning it to current. Obviously leaving values[] unchanged. Thanks everyone

4
  • 1
    You need to swap values[i] with values[i+1]. All the code does is swap the copies in current and next. Commented Dec 9, 2016 at 9:39
  • 1
    Your function doesn't modify values[i] anywhere. Commented Dec 9, 2016 at 9:41
  • @Fang You cannot pass an array by value, it decays into a pointer to its first element. Commented Dec 9, 2016 at 9:41
  • The function isn't changing the data, because it contains no code that changes the data... Surely you could figure this out yourself by spending a moment to read your own code. Commented Dec 9, 2016 at 9:58

2 Answers 2

2

The problem is that you're only modifying the function's local variables, not the array's elements.

It's the same principle as why this program will print 1 and not 2:

int main()
{
    int array[] = {1};
    int x = array[0];
    x = 2;
    printf("array[0] = %d\n", array[0]);
    return 0;
}

You need to assign values to the array's elements:

void testSort(int values[], int n)
{
    for (int i = 0; i < n-1; i++)
    {
        if (values[i] > values[i+1])
        { 
            int hold = values[i];
            values[i] = values[i+1];
            values[i+1] = hold;
        }
    }
}

Once you've fixed this, you will notice that this function only works for some inputs.
Solving that bug is left as an exercise.

Sign up to request clarification or add additional context in comments.

Comments

0

Please try below code:-

 void bubble_sort(int list[], int n){
      int c, d, t;
      for (c = 0 ; c < ( n - 1 ); c++)
      {
         for (d = 0 ; d < n - c - 1; d++)
          {
             if (list[d] > list[d+1])
             {
                 t         = list[d];
                 list[d]   = list[d+1];
                 list[d+1] = t;
             }
          }
     }
 }

3 Comments

A little explanation might help here.
first you will try this code & check is it work or not.then I will explain it
@SarikaKoli: No, that's nonsense. You are making suggestions to change the code and therefore should explain them right away, not after someone else has verifies thetsolution is correct. (That's your job as answrer, by the way: To provide working code. Whether that is by being confident or by testing actual code doesn't matter.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.