Submission 5fbe7b3e...

ChallengeInteger sorting
Submitter0x53b21be84eece2e...
Submitted at2018-45-24
Gas used964451
/**
 * This file is part of the 1st Solidity Gas Golfing Contest.
 *
 * This work is licensed under Creative Commons Attribution ShareAlike 3.0.
 * https://creativecommons.org/licenses/by-sa/3.0/
 */
pragma solidity ^0.4.24;


contract Sort {
    function qsort(uint[] mem, uint start, uint end) public pure {
        uint len = end - start;
        uint a;
        uint b;
        
        if (len < 2) return;
        if (len == 2) {
            a = mem[start];
            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) {
            a = mem[i];
            b = mem[j];
            while (a < pivot) {
                a = mem[++i];
            }
            while (b > pivot) {
                b = mem[--j];
            }

            if (i >= j) break;

            mem[i++] = b;
            mem[j--] = a;
        }

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

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

        return input;
    }
}