Radix Sort

Benchmark created on


Setup

function generate(num) {
    var result = []; for ( var idx = 0; idx < num; ++idx) { result.push(~~(Math.random()*100)); } return result;
    }
    
    function radix_sort(arr, idx_begin, idx_end, bit) {
        if (!Array.isArray(arr)) return;
        idx_begin = (typeof idx_begin === 'undefined') ? 0 : idx_begin;
        idx_end = (typeof idx_end === 'undefined') ? arr.length : idx_end;
        bit = (typeof bit === 'undefined') ? 31 : bit;
        if (idx_begin >= (idx_end - 1) || bit < 0) {
            return;
        }
        var idx = idx_begin;
        var idx_ones = idx_end;
        mask = 0x1 << bit;
        while (idx < idx_ones) {
            if (arr[idx] & mask) {
                --idx_ones;
                var tmp = arr[idx];
                arr[idx] = arr[idx_ones];
                arr[idx_ones] = tmp;
            } else {
                ++idx;
            }
        }
        radix_sort(arr, idx_begin, idx_ones, bit-1);
        radix_sort(arr, idx_ones, idx_end, bit-1);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
MSB Radix Sort
var arr = generate(10000);
radix_sort(arr);
ready
Normal Sort
var arr = generate(10000);
arr.sort();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.