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;
}
In your swap function, try passing the values as pointers.
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?
(Nov 5, 2015 10:50 AM)madera Wrote:  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?

Values are copied when they are passed to a function. This is why you have to use pointers if you want to change the original.
Read up on pointers. 🤝
By passing a pointer, the pointer itself is copied but regardless it doesn't matter -- you are passing an address to a specific location in memory where the original variable exists in the caller's scope and the address is all we care about for information, to which you can dereference and edit directly. If you pass a value, the value is a copy, you edit the copy and the copy is discarded at the return from the function with the original data remaining unchanged.

These are pointer fundamentals.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


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