Thread: Bubble sort algorithm

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Bubble sort algorithm

    Hello guys. I am a new member here. I come from greece and C programming is my activate hobbie. Lately I had been trying to implement a simple version of general bubble short algorithm. Finally I managed to run my code but I have some basically and important queries , So could you help me?

    The code is given below :

    Code:
     
    #include<stdio.h>
    #define n 10
    void bubble_sort(int x[n]);
    
    int main()
    
    {
    	int x[n]={2,3,4,5,6,1,7,9,11,2};
    	int i;
    	
    	    printf("==============");
    	    printf("Before sorting:");
    	    printf("==============");
    	
    	    for(i=0; i<n; i++)
    	    printf("\n %d " , x[i]);
    	
    	    puts("\n");
    		printf("===============");
    		printf("After sorting: ");
    		printf("===============");
    		
    		bubble_sort(x); // This is the address of array x[n] .  
    		
    		for(i=0; i<n; i++)
    		printf("\n %d",x[i]);
    
    return 0;
    }
    void bubble_sort(int x[n])
    {
    	int i,j;
    	int tmp1;
    	
    	for(i=0; i<n; i++)
    	{
    		for(j=0; j<n-1; j++)
    			{
    				
    				if(x[j]>x[j+1])
    				{
    				tmp1=x[j+1];
    				x[j+1]=x[j];
    				x[j]=tmp1;
    			    }
    				
    			}
    		}
    				
    	return;
       }

    Particularly in code line

    Code:
     bubble_sort(x); // This is the address of array x[n] .
    The x is the address of the first item of x[n] and generally is the address of x . This is call by reference??? Or call by value??? And if is call by reference how it works? Because I dont give a pointer into the function arguments only the array x[n].

    Thank you very much in advance .

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays and Pointers

    Array names, when passed to a function, are effectively pointers to their first element's type. It is a pass by value, but the value being passed is the address of the first element of the array in your case.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    Arrays and Pointers

    Array names, when passed to a function, are effectively pointers to their first element's type. It is a pass by value, but the value being passed is the address of the first element of the array in your case.


    Quzah.
    Thank you very much for the speedy answer and also for the link - source . It is a pass by value of pointer? And actually then call by reference? Because from theory I know that if you have an address in your call function (and we have an address of array) then you have a call by reference :/ It is little complicated for this example... Do you agree with my thought?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C doesn't actually have references. What you call pass by reference is really passing the value of an address that you then use as a pointer. It's still technically a value pass.
    Code:
    #include<stdio.h>
    void foo( int array[], size_t s )
    {
        size_t x = 0;
        for( x = 0; x < s; x++ )
            array[ x ] = x + s;
    }
    int main( void )
    {
        int array[2];
        foo( array, 2 );
        printf( array[0,1] is: %d,%d\n", array[0], array[1] );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    C doesn't actually have references. What you call pass by reference is really passing the value of an address that you then use as a pointer. It's still technically a value pass.
    Code:
    #include<stdio.h>
    void foo( int array[], size_t s )
    {
        size_t x = 0;
        for( x = 0; x < s; x++ )
            array[ x ] = x + s;
    }
    int main( void )
    {
        int array[2];
        foo( array, 2 );
        printf( array[0,1] is: %d,%d\n", array[0], array[1] );
        return 0;
    }
    Quzah.
    Yes but only if we assume the hexadecimal value(the adress of variable) as a real value(real value in my opinion is something which appears often in humans such as a decimal value :P ) . OK then we have a call by value because and this value is also a value with the only difference that it has a hexadecimal representation. Do you actually mean this?

    And something else... "C doesn't actually have references" => hmmmmm... I am confusing right now because i have learnt references on C :P might you mean some updates of the whole of C language which not defines the references anymore??? Or it have defined (that the C language has not actually references) in the long past?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.Lnx View Post
    Yes but only if we assume
    No, not if only you assume. Everything is a value in C.
    Quote Originally Posted by Mr.Lnx View Post
    And something else... "C doesn't actually have references" => hmmmmm... I am confusing right now because i have learnt references on C :P might you mean some updates of the whole of C language which not defines the references anymore??? Or it have defined (that the C language has not actually references) in the long past?
    That must be it. I must have no idea what I'm talking about, having been on this forum for over ten years.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Mr.Lnx View Post
    [..some random garbage...]

    And something else... "C doesn't actually have references" => hmmmmm... I am confusing right now because i have learnt references on C :P might you mean some updates of the whole of C language which not defines the references anymore??? Or it have defined (that the C language has not actually references) in the long past?
    C never was, nor ever will be pass by reference. You may have been in fact taught the concept of passing by reference by using pointers; however you were never taught pass by reference in C.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by quzah View Post
    No, not if only you assume. Everything is a value in C.That must be it. I must have no idea what I'm talking about, having been on this forum for over ten years.


    Quzah.
    You are right. I had a fault expression when I was saying "Only if we assume" . The physical address in C is a value... and we can actually prove this if we have a pointer and we want to show from printf , then we can use the command =>

    Code:
     printf("%p", *p); // actually will display a hexadecimal value , well I m ok
    Last question for understanding clearly your beliefs about pointers

    if I write

    Code:
     
    #include<stdio.h>
    void foo(int *n);
    int main()
    
    int n=10; // For instance 
    
    printf("Before foo : %d", n);
    
    foo(&n); 
    printf("After foo: %d", n);
    
    
    return 0; 
    }
    void foo(int *n)
    {
    *n=5;
    
    }
    Ok. Here on this code the function call foo(&n); is pass by value for you? Or pass by reference... if your answer is "Is pass by value because the element &n is a value (meaning hexadecimal values) " then we are ok. Because we tell the same thing but with other words

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Mr.Lnx View Post
    Ok. Here on this code the function call foo(&n); is pass by value for you? Or pass by reference... if your answer is "Is pass by value because the element &n is a value (meaning hexadecimal values) " then we are ok. Because we tell the same thing but with other words
    Yes, you guys are then talking about the same thing. This is an important concept to clearly understand, especially when dealing with passing pointers "by reference", e.g. the necessity of a pointer to a pointer. A minor suggestion would be to improve your functional English with specific regards to programming language terms and concepts.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The function foo takes a value that represents the address of an integer. It's still a value. You can prove this by changing it, and then seeing if it keeps outside the function. Just like this is a value pass:
    Code:
    #include<stdio.h>
    void foo( int x )
    {
        x = 5;
    }
    int main( void )
    {
        int y = 6;
        foo( y );
        printf( "%d\n", y ); /* if this were really a reference pass, y would be 5 */
        return 0;
    }
    You can substitute 'int' for anything you want, and the same thing will happen.
    Code:
    void bar( int *p )
    {
        p++; 
    }
    ...
    int *x = NULL;
    bar( x );
    ...
    x still points to the same place it did before you sent it to the function.


    Quzah.
    Last edited by quzah; 09-15-2011 at 07:19 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Is there any difference with the meanings of pass by refrence

    and passing by reference????

    sorry guys but I am trying to practice my english...

    I was studying at University of Peiraius and I haven't big idea for the english terminologies but I am trying to learn

    I am thinking that we discussing for the same thing but we put other words to describe the same thing
    Last edited by Mr.Lnx; 09-15-2011 at 07:20 PM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think of ONLY "passing by copy", in C. If n is an int, for instance, the call:

    foo(n)

    n will be a COPY of the n in the function that is calling foo().

    If the call was:

    foo(&n)

    &n will be a COPY of the address of n.

    It just so happens, that the COPY of the address of n, is still a valid pointer to n itself.

    So in C, ALL parameters to functions, are passed by COPYING. It just so happens that some of those parameters are addresses, and those can mimic a call by reference (and are usually called that).

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a distinction in C++ for example, because they have references, and you can pass a pointer (as a reference):
    Code:
    foo( x ); /* pass x to foo, lets say foo takes a reference */
    bar( &x ); /* pass a pointer to it ... pass a reference to */
    The difference is I guess in describing what the function takes. Foo takes a reference as an argument. Bar takes a reference (a pointer) to whatever x is.

    C makes no distinction, because everything is a value. So if someone says in C that they are passing a reference to something, we understand that they mean they are passing a pointer to it. But it's not really a reference when compared to other language that have actual references.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Ok !!! It was my fault... I meant call by reference and call by value...

    NOw.. we are ok???

    When I am saying "call by reference" I mean that I manipulate the variable from it's memory and the changes are permanent. Have I fault?

    SOrry if I cause headache and I am really grateful for your time.
    Last edited by Mr.Lnx; 09-15-2011 at 07:32 PM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mr.Lnx View Post
    Yes but only if we assume the hexadecimal value(the adress of variable)
    The others are giving you very good advice... C is strictly pass by value, there are no references in C... Even a pointer is passed as a copy of the value of the pointer... not the pointer itself. In C *everything* is a number, there's no magic.

    However, there seems an additional confusion about hexidecimal... This, just like the decimal numbers we use is merely a convenient representation of the underlying binary values the computer runs on... Any number can be represented in different number bases, hex is not for addresses only, it is a genera purpose representation. That we usually print addresses in hex is merely a programming tradition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. testing a bubble sort algorithm
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 01:00 AM
  2. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-23-2008, 12:03 AM
  3. Help, bubble-sort algorithm
    By webznz in forum C Programming
    Replies: 6
    Last Post: 10-30-2007, 01:28 AM
  4. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM