Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Bubble Sort C program

I'm not sure what I'm doing wrong with this program...first and foremost it gets locked in an infinite loop whenever the function "swap()" is called. I'm used to java so its possible im overlooking something simple 🤮

Code
#include <stdio.h>
#include <stdlib.h>


int swap(int arg0, int arg1)
{
    int temp;
    
    temp = arg0;
    arg0=arg1;
    arg1=temp;
    
    return 0;
}


int main(int argc, const char * argv[]) {
    int i;
    int arr[i];
    int newi;
    
    printf("input number of elements in list: \n");
    scanf("%i", &i);
    
    int origi = i;
    
    
    for (int j=0; j < i; j++)
    {
        printf("input element: \n");
        scanf("%i", &arr[j]);
    }
    
    
    while(i!=0)
    {
        newi = 0;
        for (int x=0; x<i; x++)
        {
            if (arr[x-1] > arr[x])
            {
                    swap(arr[x],arr[x-1]);
                    newi = i;
            }
        }
        i = newi;
    }
    
        
    
    
    printf("sorted list is:\n");
    for (int k = 0; k < origi; k++)
        printf("%i \n", arr[k]);
    
    return 0;
}
Thanks for the replies, I fixed it :)

Code
#include <stdio.h>
#include <stdlib.h>


int swap(int *arg0, int *arg1)
{
    int temp;
    
    temp = *arg0;
    *arg0=*arg1;
    *arg1=temp;
    
    return 0;
}


int main(int argc, const char * argv[]) {
    int i;
    int newi;
    
    printf("input number of elements in list: \n");
    scanf("%i", &i);
    
    int arr[i];
    int origi = i;
    
    
    for (int j=0; j < i; j++)
    {
        printf("input element: \n");
        scanf("%i", &arr[j]);
    }
    
    
    while(i!=0)
    {
        newi = 0;
        for (int x=0; x<i; x++)
        {
            if (arr[x-1] > arr[x])
            {
                    swap(&arr[x-1], &arr[x]);
                    newi = i;
            }
        }
        i = newi;
    }
    
        
    
    
    printf("sorted list is:\n");
    for (int k = 0; k < origi; k++)
        printf("%i \n", arr[k]);
    
    return 0;
}


Any explanation as to why I had to use pointers instead of an immediate array value?
lmao exact same example...ok, I get that. What I still don't understand is why direct (non pointer) variables are not "permanently" changed when they are passed to a function? I'm sure its possible that the (non pointer) variables being passed to the function are stored in a stack before they are passed to the function so that they can be retrieved when the function is returned.
More specifically, is this quirk just a characteristic of the C language in order to make it more versatile?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by synboxinDec 19, 2018 03:08 AM 9 418 Dec 19, 2018 03:08 AM
Last Post: itcorp
[.NET] by v.e.n.o.mDec 9, 2017 11:24 AM 18 827 Dec 9, 2017 11:24 AM
Last Post: v.e.n.o.m
[C] by SigmaMay 2, 2017 03:54 PM 7 188 May 2, 2017 03:54 PM
Last Post: Sigma