Hacker News new | past | comments | ask | show | jobs | submit login

javascript version (faster):

    var input = [5, 3, 6, 3, 6, 3, 1, 4, 7];

    var timeAxis = [];
    // Put every element to the corresponding time point.
    // Time point would hold an array, as we can have
    // equal elements.
    for (var i = 0; i < input.length; i++) {
        elem = input[i];
        timeAxis[elem] = [elem].concat(timeAxis[elem] || []);
    }

    // skip all empty (UNDEFINED) time points
    // and contact all the arrays:
    var sorted = timeAxis.reduce(function (accum, cur) {
                                     return cur ? accum.concat(cur) : accum},
                                []);
    console.log(sorted)



The speedup is from usage of _virtual time_, which we don't need to wait for.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: