What are efficient ways to sort arrays that have mostly a small set of duplicated elements? That is, a list like:
{ 10, 10, 55, 10, 999, 8851243, 10, 55, 55, 55, 10
Implementation in C++ based on algo as suggested by @Antti Huima
#include #include // Modifies input array to a sorted array // Complexity: O(n+(k*log(k))) where 'k' = number of unique elements input array template void SortArrayWithDuplicates(std::vector& in_seq) { std::unordered_map key_counts_map; // Count freqs O(n) for (const auto& itr: in_seq) key_counts_map[itr] += 1; // Sort elements by inserting into a map O(k*log(k)) std::map key_counts_sorted_map; for (auto const& itr: key_counts_map) key_counts_sorted_map.insert(std::make_pair(itr.first, itr.second)); auto AlwaysTrue = [](Datatype i)->bool{return true;}; auto seq_itr = std::begin(in_seq); // Update input sequence with new sorted values for (auto const& itr: key_counts_sorted_map) { std::replace_if(seq_itr, seq_itr+itr.second, AlwaysTrue, itr.first); seq_itr += itr.second; } }