The Full Counting Sort

  • + 2 comments

    After several tests and 11 submissions, some of them failed and some other passed. This was my first original code with the Kakason's upgrades that passes all test cases.

    function processData(input) {
        input = input.trim().split('\n');
        var n = parseInt(input[0]);
        var numbers = [];
        for (var i = 0; i < 100; i++) {
            numbers[i] = [];
        }
        var half = n / 2;
        for (i = 1; i <= n; i += 1) {
            var obj = input[i].split(' ');
            numbers[parseInt(obj[0])].push(i <= half ? '-' : obj[1]);
        }
        for (i = 0; i < numbers.length; i += 1) {
            for (var j = 0; j < numbers[i].length; j += 1) {
                process.stdout.write(numbers[i][j] + ' ');
            }
        }
    }
    

    In conclusion, YES, concatenating the resulting string is too big for this environment that it doesn't fit, and in those cases is better to use process.stdout.write to print in-line instead of console.log because this adds breaking lines.

    I really hope this can help others JS developers, and please thank Kakason, I couldn't do it without him.