#include <stdio.h>

#define ARRAYSIZE 6
#define MAX 4

int main()
{
    int arr[] = {2, 1, 3, 2, 1, 4};
    int final[ARRAYSIZE];

    // Given we know max element in array
    // This technique is based on counting sort
    int count[MAX + 1] = {0};
    int i = 0;
    int j = 0;

    for (i = 0; i < ARRAYSIZE; i++) {
        count[arr[i]]++;
    }

    for (i = 0; i < MAX + 1; i++) {
        if (count[i] != 0) {
            while (count[i] != 0) {
                final[j++] = i;
                count[i]--;
            }
        }
    }

    printf("Sorted Array\n");
    for (i = 0; i < ARRAYSIZE; i++) {
        printf("%d ", final[i]);
    }

    return 0;
}
