# Bubble sort algorithm

This is a discussion on Bubble sort algorithm within the C Programming forums, part of the General Programming Boards category; Hello guys. I am a new member here. I come from greece and C programming is my activate hobbie. Lately ...

1. ## 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. 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.

3. Originally Posted by quzah
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. 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.

5. Originally Posted by quzah
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. Originally Posted by Mr.Lnx
Yes but only if we assume
No, not if only you assume. Everything is a value in C.
Originally Posted by Mr.Lnx
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.

7. Originally Posted by Mr.Lnx
[..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.

8. Originally Posted by quzah
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`

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. Originally Posted by Mr.Lnx
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.

10. 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.

11. 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

12. 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. 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.

14. 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.

15. Originally Posted by Mr.Lnx
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.

Page 1 of 3 123 Last