The passed by reference argument is a shared item between caller and callee.
Is this item in sharing for recursive calls of a function ?
Suppose this merge sort algorithm:
MergeSort(ref int[]S)
{
.
.
.
MergeSort(ref S1);
MergeSort(ref S2);
Merge(S1,S2,S);
}
By tracing algorithm for S={2,3,1,0} , we have :
2,3,0,1
2,3 1,0
2 3 1 0
At the end of algorithm ,compiler will do a merge into S in second level of tree, that merges(0,1) and (2,3) and creates (0,1,2,3).
We have just a one S array that is shared , How the compiler merge 2 of S arrays and merged them ?
So S is not sharing??
Thanks in advance
The object is passed by reference BUT in every recursive call, the variable
Spoints [refers] to a different object. Thus, everymerge()is actually preformed on a different object.