Submission ea18218d...

ChallengeInteger sorting
Submitter0x53b21be84eece2e...
Submitted at2018-14-24
Gas used1036988
pragma solidity ^0.4.24;

contract Sort {
    function qsort(uint[] mem, uint start, uint end) public pure {
        uint len = end - start;
        if (len < 2) return;
        if (len == 2) {
            uint a = mem[start];
            uint b = mem[start + 1];
            if (a > b) {
                mem[start] = b;
                mem[start + 1] = a;
            }
            return;
        }

        uint pivot = mem[(start + end) / 2];
        uint i = start;
        uint j = end - 1;
        while(true) {
            while (mem[i] < pivot) i++;
            while (mem[j] > pivot) j--;

            if (i >= j) break;

            uint temp = mem[i];
            mem[i] = mem[j];
            mem[j] = temp;
            
            i++;
            j--;
        }

        qsort(mem, start, i);
        qsort(mem, i, end);
    }

    function sort(uint[] input) public pure returns(uint[]) {
        qsort(input, 0, input.length);
        return input;
    }
}