Skip to content
Snippets Groups Projects
Commit 4adf12fd authored by Dmitri Naumov's avatar Dmitri Naumov Committed by Tom Fischer
Browse files

Add zip-sort-unzip version of quicksort with permutations.

parent 172020a3
No related branches found
No related tags found
No related merge requests found
......@@ -111,6 +111,35 @@ void quicksort(T1* array, std::size_t beg, std::size_t end, T2* second_array)
namespace BaseLib
{
template <typename T1, typename T2 = std::size_t>
void quicksortD(std::vector<T1>& array, std::size_t beg, std::size_t end, std::vector<T2>& perm)
{
// Zip input arrays.
std::vector<std::pair<T1, T2>> data;
data.reserve(array.size());
std::transform(array.begin(), array.end(), perm.begin(),
std::back_inserter(data),
[](T1 const& t1, T2 const& t2)
{
return std::make_pair(t1, t2);
});
// Sort data using first element of the pair.
std::sort(data.begin(), data.end(),
[](std::pair<T1, T2> const& a, std::pair<T1, T2> const& b)
{
return (a.first < b.first);
});
// Unzip sorted data.
for (std::size_t i = 0; i < data.size(); i++)
{
array[i] = data[i].first;
perm[i] = data[i].second;
}
}
template <typename T1, typename T2 = std::size_t>
class Quicksort
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment