How to start from a particular element in an array

Elad

Member
Joined
Feb 15, 2020
Messages
13
Programming Experience
1-3
hello!
I want to build a Merge Sort sort, but I ran into a pretty annoying problem. In C++ or even in C I send an array or pointer to a function the pointer is always pointing to the first member so it is very easy to send to the recursive function the array every time in some member and not necessarily in the first member of the original array, or in other words you can promote a pointer. In C# life is supposed to be simple but I see no way to send the array to a function so that the array starts from a particular member so I can not promote the "Pointer" (in c # I know that Pointers are something different from the Pointers in c ++ or C) of The array.
If I can still send the array to a function and recursively can I somehow make a kind of promotion to the array?
I know there are examples of merge sort in c # but all the examples I saw are very cumbersome compared to the code in C++
The following is an example ():

C#:
static void mergeSort(int[] arr, int p,int [] helper)
{
int left = p / 2;
int right = p - left;
if (p < 2)
{
return;
}
//In c # it does not work that way:
mergeSort(arr + left, p, helper);
//...Missing continuation of the code
}
 
Last edited by a moderator:

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,877
Location
UK
Programming Experience
10+
This is the last time I will edit your posts to add code tags. If you don't put your code in code tags in future, you will find I will start closing your topics without warning.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,393
Location
Chesapeake, VA
Programming Experience
10+
If you are using .NET Core 2.1 or higher, use Span<T> (or Memory<T>) so that you can keep advancing a slice. If you are stuck in older .NET Framework, you can use the ArraySegment<T> instead.

Or if you are willing to use the /unsafe compiler flag, then you can use pointers all you want.
 

Elad

Member
Joined
Feb 15, 2020
Messages
13
Programming Experience
1-3
This is the last time I will edit your posts to add code tags. If you don't put your code in code tags in future, you will find I will start closing your topics without warning.
This is the last time I will edit your posts to add code tags. If you don't put your code in code tags in future, you will find I will start closing your topics without warning.
I'm really sorry but the tag does not work for me,
I have tried several times but every time I get a message that a code or something like that is missing, even though the code is perfectly fine.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,877
Location
UK
Programming Experience
10+
CodeTags.gif

It's really simple and there are no problems with the code tags button. You simply do as the screenshot demonstrates and post your code in the box that appears after you select Code Tags from the drop down. Then click Continue, and leave the formatted text alone. It works for everyone else.
 

Elad

Member
Joined
Feb 15, 2020
Messages
13
Programming Experience
1-3
View attachment 1090
It's really simple and there are no problems with the code tags button. You simply do as the screenshot demonstrates and post your code in the box that appears after you select Code Tags from the drop down. Then click Continue, and leave the formatted text alone. It works for everyone else.
Well thanks I got it!
The truth is I actually saw a button of 'tags' and thought you were talking about it
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,393
Location
Chesapeake, VA
Programming Experience
10+
In C++ or even in C I send an array or pointer to a function the pointer is always pointing to the first member so it is very easy to send to the recursive function the array every time in some member and not necessarily in the first member of the original array, or in other words you can promote a pointer. In C# life is supposed to be simple ...
Actually, in C and C++, arrays devolve into pointers, not promoted into pointers. I don't know why you consider this C/C++ behavior simpler, because it actually makes things more complicated for a programmer because he/she now needs to know that this kind of devolution is happening. Also consider that in C/C++, now the programmer needs to know also pass along the size of the array because the pointer is not going to provide enough information about the size of the array. In C#, arrays are truly objects and you can pass arrays as arrays which know their own size.
 
Top Bottom