Tell the algorithm (in words) to sort the array by the elements of the third column.
- Sorry, specify what you need to sort and where did you get such an algorithm? I know a lot of sorts, but I haven't heard about this ... - BogolyubskiyAlexey
- This is something like sorting a table. For example, by name or by last name, while other columns are adapted to be sorted. - nullptr p.m.
- The campaign means a multidimensional array. - Specter
- Yes it is. - nullptr p.m.
- 2I understand that there is an array: {{0,1, 2} {1,2, 0} {2,0, 1}} you need to sort it by the third column, ie: {{1,2, 0} {2.0, 1} {0,1, 2}} do you understand correctly? - Specter
|
3 answers
if you want to sort, as suggested by @Spectre , then there are no special problems - we take as a basis any kind of sorting of a one-dimensional array.
We compare the elements T [i] [3] and T [j] [3], and, if necessary, change the i-th and j-th line.
|
For example, for the array int x [n] [m]; can
qsort(x,n,sizeof(int)*m,xcmp); .... int xcmp (const void *a, const void *b) { int *ax = (int *)a, *bx = (int *)b; return ax[3]-bx[3]; }
- Just do not forget to bring x to void * - skegg
|
Based on the answer from @avp, I suggest this function for comparison.
int xcmp (const void *a, const void *b) { return (int*)a[2]-(int*)b[2]; }
Note - index == 2, not 3!
And for universalism, you can generally make a template to apply for any types.
|