Hi,

I'm trying to implement counting sort with ranges; however, hit a roadblock here while testing. I'm using the following code:

    int[1_000_000] arr;

    void main() {
        import std.conv : to;
        import std.datetime;
        import std.random;
        import std.stdio;

        for(int i; i < arr.length; ++i)
            arr[i] = uniform(0, 1000);

auto benchmarks = benchmark!(algorithmSort, countingSort)(1);

        writeln("Agorithm's Sort: ", to!Duration(benchmarks[0]));
        writeln("Counting Sort: ", to!Duration(benchmarks[1]));
    }

    void algorithmSort() {
        import std.algorithm : sort;

        auto result = arr[].sort;
    }

    void countingSort() {
        import std.algorithm : count, map;
        import std.range : array, chain, iota, repeat;

auto result = 1_000.iota.map!(a => a.repeat(count(arr[], a))).chain.array;
    }

1. `arr[].sort` is changing arr in place. Any way to not do that?
2. I noticed that result within `countingSort` is an array of arrays. I thought `chain` would concat them... did I miss something obvious? 3. It's kind of hard to compare performance because of (1), but is there a better way to do this?

Reply via email to