for Robot Artificial Inteligence

6. Counting Sort

|

#include <bits/stdc++.h>

using namespace std;

ifstream f("data.in");
ofstream g("data.out");

int A[101], X, n; // supposed that we have numbers from 0-100

int maximum;

int main()
{
    f>>n; //number of elements
    for (int i=1; i<=n; ++i)
    {
        f>>X; // a New number
        ++A[X]; // increasing the appearance array

        maximum = max(maximum, X);
    }

    for (int i=0; i<=maximum; ++i)
    {
        if(A[i]>0){
            for (int j=1; j <= A[i]; ++j)
                cout<<i<<" ";
        }
    }
}
  • data 셋중에 숫자가 겹치는 것들을 heap을 이용하여서 counting.

  • 제일 많은 max값을 구한 것을 꺼내서, cout.

Comments