1

I have to write a method within already-written code that passes me an array directly. However once inside my method that array has become a pointer to the first object in the array. So now I have done some calculations and want to sort the array. But since it's now not considered an array, I can't perform the sort() function.

What's the best way to sort an array when I only have the pointer to work with?

  • As long as you have the size as well (which any function working on an array should have), sure you can use `std::sort`. – chris Apr 13 '14 at 05:13
  • It depends on what kind of array and what kind of sort you would like to achieve? Quicksort, heapsort, bubblesort, etc? – László Papp Apr 13 '14 at 05:14
  • Why do you say "it is not considered an array"? The pointer to the first element is the usual way to pass an array. The only other thing you need is the length. Make sure you tag your question as either C or C++ - they are not the same language. Are you writing your own sort function? – Floris Apr 13 '14 at 05:14

4 Answers4

1

You either need to know the number of elements in the array, passed as a separate parameter or have a pointer to one past the last element.

void my_sort(int* p, unsigned n) {
    std::sort(p, p+n);
}

or

void my_sort2(int* p, int* p_end) {
    std::sort(p, p_end);
}

and you would call them

int a[] = { 3, 1, 2 };
my_sort(a, sizeof a / sizeof a[0]); // or 3...
my_sort2(a, &a[2] + 1); // one past the last element! i.e. a+3
ppl
  • 1,120
  • 5
  • 18
  • Ah yeah, I do know the size. Didn't realize sort worked liked that, thanks! – user2155320 Apr 13 '14 at 05:25
  • 1
    @user2155320: please consider using std::array and higher level containers rather than raw array. – László Papp Apr 13 '14 at 05:26
  • Also, if you need stable sort, [stable_sort](http://www.cplusplus.com/reference/algorithm/stable_sort/) will be better than sort. – László Papp Apr 13 '14 at 05:31
  • @MattMcNabb: I do not follow... the three lines can be within a function without additional two functions, commenting or "or'ing" the pbegin and pend" variant. Also, at the very least the functions should be inline this way. It is confusing to have two additional functions just to call sort. – László Papp Apr 13 '14 at 05:59
  • He wants the array to have been passed by parameter to a function. Writing `int a[] = { 3, 1, 2}; std::sort(a, a+3);` wouldn't do that. That's why the sample code passes `a` to a function, and then the function sorts. – M.M Apr 13 '14 at 06:11
  • @MattMcNabb: I am still not sure why anyone would use the second version... it adds more confusion than gain IMHO, but it is just my personal two cents, and it is fine if we disagree. So long as it helped the OP, it is fine either way. – László Papp Apr 13 '14 at 07:25
  • Usually, it is start, start+size IMHO, but on the other hand, raw arrays are not constructive altogether in C++. ;) – László Papp Apr 13 '14 at 16:20
  • @LaszloPapp The second version is what you advocate in your last comment and what you use in your own post. – ppl Apr 13 '14 at 16:37
0

In c there is essentially no difference between an "array" and a "pointer to the first object in the array". Arrays are referred to using their base pointer, that is, pointer to first object. Technically precise explanation at Array base pointer and its address are same. Why? So, just sort the array as you would anywhere else. Got an example sort or sample code in mind or is that sufficient?

Community
  • 1
  • 1
RobP
  • 9,144
  • 3
  • 20
  • 33
  • There's a world of difference between an array, and a pointer to its first element. One is an lvalue and one isn't. Also, try doing `sizeof` on each. – M.M Apr 13 '14 at 06:10
  • I almost added "except one carries size information"... but for the purposes of this question, they behave very much the same. – RobP Apr 13 '14 at 06:16
0

The best would be if you could start using std::array from C++11 on:

http://en.cppreference.com/w/cpp/container/array

This way, you would also have the size known and accessible by the corresponding size method. You could also consider other std container types rather than raw array. In general, it is better to avoid raw arrays as much as possible.

Failing that, you would need to know the size of the array either through function parameter, or other means like class member variable if it is happening inside a class, and so on.

Then, you could use different type of sorting algorithms based on your complexity desire; let it be quick sort, bubble sort, heap sort, stable sort, etc... it depends on what kind of data, the array represents, etc.

One sorting algorithm is to use std::sort. Therefore, you would be writing something like this:

std::sort (mystdarray.begin(), mystdarray.end()); 

or

std::sort (myrawarray, myrawarray+size); 
László Papp
  • 51,870
  • 39
  • 111
  • 135
0

Sort it exactly as you would sort it before you passed it in. If your sort() function requires a length, then pass the length as an additional parameter.

merlin2011
  • 71,677
  • 44
  • 195
  • 329