Counting Sort 2

  • + 0 comments

    C++ (more at https://github.com/IhorVodko/Hackerrank_solutions/tree/master/Algorithms , feel free to give a star :) )

    std::vector<int> countingSort(std::vector<int> const & _nums) {
        using namespace std;
        vector<int> frequencies(100, 0);
        for(auto & num : _nums){
            ++frequencies.at(num);
        }
        vector<int> sorted;
        sorted.reserve(1'000'000);
        size_t count = 0;
        for(auto & frqnc : frequencies){
            while(frqnc--){
                sorted.emplace_back(count);
            }
            ++count;
        }
        sorted.shrink_to_fit();
        return sorted;
    }