Jincheol Park
Jincheol Park

Reputation: 91

How to keep while loop in bubble sort function in C

I'm trying to make my own bubble-sort function in C.

As you can see in the code below this, I'm trying to only using while / if loop to create this function. I put 5 numbers (1,3,2,5,4) so that size of array of this would be 5, and I got 5 (I checked it with Python(C)tutor. However, It works well until tab[j] gets 3. I'm trying to figure it out, but couldn't figure it out why it keeps going out when tab[j] gets 3.

Could you anybody explain what's wrong to me? I would appreciate it.

Here is my code below:

#include <stdio.h>

void ft_sort_integer_table(int *tab, int size)
{
    int i;
    int j;
    int tem;

    i = 0;
    j = 0;
    while(tab[i] < size)
    {
        if(tab[j] > tab[j+1])
        {
            tem = tab[j];
            tab[j] = tab[j+1];
            tab[j+1] = tem;
            printf("%d ", tab[j]);
            j++;
        }
        else if(tab[j] < tab[j+1])
        {
          printf("%d ",tab[j]);
          j++;
        }
        i++;
    }
}

int main(void)
{
    int tab[] = {1,3,2,5,4};
    int size = sizeof(tab)/sizeof(*tab);
    ft_sort_integer_table(tab, size);
    return(0);
}

Upvotes: 1

Views: 1019

Answers (2)

ggorlen
ggorlen

Reputation: 56983

You'll need an inner loop in your bubble sort, which is responsible for moving the largest element to the back and performing swaps i times (these large elements are "bubbling up"). Start the inner loop at 0 on each iteration and iterate through size - i (we know that the last i elements are sorted and in their final positions).

i controls your outer loop and should be incremented at the end of the loop (just as you would with a for loop). j controls the inner loop and should be incremented at the end of the loop.

While you're at it, it's a good idea to move your printing out of the sort function, which causes an unnecessary side effect and might frustrate your debugging efforts.

Also, it's worth mentioning that (1) for loops are more semantically appropriate here and (2) there is an optimization available by adding a boolean--as soon as you have a pass through the inner loop that performs no swaps, end early!

#include <stdio.h>

void ft_sort_integer_table(int *tab, int size)
{
    int i = 0, j, tem;

    while (i < size)
    {
        j = 0;

        while (j < size - i)
        {
            if (tab[j] > tab[j+1])
            {
                tem = tab[j];
                tab[j] = tab[j+1];
                tab[j+1] = tem;
            }

            j++;
        }

        i++;
    }
}

int main(void)
{
    int tab[] = {1,3,2,5,4,6,7,1,5,6,8,9,1,4,5,1,2};
    int size = sizeof(tab) / sizeof(*tab);
    ft_sort_integer_table(tab, size);

    for (int i = 0; i < size; i++) 
    {
        printf("%d ", tab[i]);
    }

    return(0);
}

Output:

1 1 1 1 2 2 3 4 4 5 5 5 6 6 7 8 9

Upvotes: 3

dee cue
dee cue

Reputation: 1053

I'm trying to figure it out, but couldn't figure it out why it keeps going out when tab[j] get 3.

From your code above, j increment in the same fashion as i. That means both variables will have the same value since j will be incremented by one after the if-then-else statement, and i will also be incremented by one at the end of each loop. Therefore, tab[j] is referencing the same value as tab[i]

With that being said, the boolean condition in the while loop checks whether the value in the tab[i] is less than the value of size.

When i == 3, tab[i] == 5 since in the loop, only the values in the array of index less then i are swapped/changed. Since the size variable holds that value of 5, tab[i] < size will result in a false value and exit the loop.

More information on bubble sort can be found here, https://www.geeksforgeeks.org/bubble-sort/

Upvotes: 0

Related Questions