-1

Because of calling recursive function don't do printf (bubble sort)? (actually only first time)

#include <stdio.h>

int* bubblesort(int n1, int arr[])
    {
        for (int i = 0; i < n1 - 1; n1++)
            {
                if (arr[i] > arr[i+1])
                {
                    printf("Replace %i whith %i\n", arr[i], arr[i+1]); // QUESTION?? Because of calling recursive function don't do printf?
                    int buffer = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = buffer;

                }
            }

        while (n1 > 1)
            bubblesort(n1-1, arr);

        return arr;
    }

int main (void)
{
int array1[5];
array1[0] = 3;
array1[1] = 2;
array1[2] = 5;
array1[3] = 8;
array1[4] = 7;
int n = 5;
//printf("size %i array 1 %i", n, array1[0]);

int* identifier_of_array = bubblesort(n, array1);

printf("*****************\n\n");

for (int i = 0; i < 5; i++)
{
    array1[i] = *(identifier_of_array + i);
    printf("sorted array #%i is: %i\n", i , *(identifier_of_array+i));
}

}

update:

I have changed my code, but it is still not working.

// Undone not it wants working . 

#include <stdio.h>

int* bubblesort(int n1, int arr[])
        {
            for (int i = 0; i < (n1 - 1); n1++)
                {
                    if (arr[i] > arr[i+1])
                    {
                        printf("Replace %i whith %i\n", arr[i], arr[i+1]);
                        int buffer = arr[i];
                        arr[i] = arr[i+1];
                        arr[i+1] = buffer;

                    }
                }

            while (n1 > 1)
                {
                    int * identifier = bubblesort((n1-1), arr);
                    for (int i1 = 0; i1 < 5; i1++)
                        {
                            arr[i1] = *(identifier + i1);
                            printf("sorted array #%i is: %i\n", i1 , *(identifier+i1));
                        }
                }
            return arr;
        }

int main (void)
{
    int array1[5];
    array1[0] = 3;
    array1[1] = 2;
    array1[2] = 1;
    array1[3] = 8;
    array1[4] = 7;
    int n = 5;
    //printf("size %i array 1 %i", n, array1[0]);

    int* identifier_of_array = bubblesort(n, array1);

    printf("*****************\n\n");

    for (int i = 0; i < 5; i++)
    {
        array1[i] = *(identifier_of_array + i);
        printf("sorted array #%i is: %i\n", i , *(identifier_of_array+i));
    }

}
1
  • please do not post updates/follow-ups as answers! you may edit this post! also you should probably format your code well and put comments on it to make it easier to understand! – Kareem Nov 2 '15 at 13:02
0

Your original code had a couple structural problems. THe for loop was incrementing n1 instead of i. The recursive call was a while loop, when it should have been an if statement, and the first parameter of the recursive call, n1, needed to be decremented before the call. I'll leave it to you to figure out all of the ramifications of these errors. Here's the revised code:

int* bubblesort(int n1, int arr[])
    {
        for (int i = 0; i < n1 - 1; i++)  // changed n1++ to i++
            {
                if (arr[i] > arr[i+1])
                {
                    printf("Replace %i whith %i\n", arr[i], arr[i+1]); 
                    // QUESTION?? Because of calling recursive function don't do printf?
                    int buffer = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = buffer;

                }
            }

    for(int z=0; z<n1; z++) printf("     Arr[%i]=%i\n", z, arr[z]);

        if (n1 > 1)   // changed to if statement
            bubblesort(--n1, arr);   //need to decrement n1 before next call

        return arr;
    }

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

1
  • Thanks for a job. About decrementing - on my comp works with (n-1) in braces too. may be each recursive re-gives value for n1? Sorry, what do you mean ramifications? (for program?) – Oleksandr Kozlov Nov 2 '15 at 11:53

You must log in to answer this question.

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