c# - Quicksort of 3D array based on another 1D array -
i have 3d array containg values , want sort based on values listed in 1d array. example,
the 3d array has values of:
1 2 3 4 5 6 7 8 9
and 1d array has values of:
20 11 12
so if considered 3d array related 1d array (rows related each other), result want in 3d array is:
4 5 6 7 8 9 1 2 3
i have searched quicksort algorithm, couldn't find want.
you can implement "argument quicksort" returns indices sort array quite easily. here implementation in c++:
#include <algorithm> template <class indexcontainer, class datacontainer> void arg_qsort(indexcontainer& indices, const datacontainer& data, int left, int right) { int = left; int j = right; int pivot = left + (right - left) / 2; while (i <= j) { while (data[indices[i]] < data[indices[pivot]]) ++i; while (data[indices[j]] > data[indices[pivot]]) --j; if (i <= j) { std::swap(indices[i], indices[j]); ++i; --j; } } if (left < j) arg_qsort(indices, data, left, j); if (i < right) arg_qsort(indices, data, i, right); } /// /// compute indices sort given data. /// template <class indexcontainer, class datacontainer> void argsort(indexcontainer& indices, const datacontainer& data) { int size = indices.size(); if (size == 0) return; (int = 0; < size; ++i) { indices[i] = i; } arg_qsort(indices, data, 0, size - 1); }
now can compute order of rows in 2d array using argsort
. example, argsort
return 1 2 0
.
Comments
Post a Comment