Submission 4a2c1a90...

ChallengeInteger sorting
Submitter0x6b9205614d2523d...
Submitted at2018-19-08
Gas used1000861
pragma solidity 0.4.24;


contract Sort {

    function sort(uint[] input) public pure returns(uint[]) {
        if (input.length == 0)
            return;
        sort(input, 0, input.length - 1);
        return input;
    }

    function sort(uint[] arr, uint left, uint right) internal pure {
        uint i = left;
        uint j = right;
        uint pivot = 0;
        assembly {
            pivot := mload(add(add(arr, 0x20), mul(0x20, add(left, div(sub(right, left), 2)))))
        }
        while (i <= j) {
            while (arr[i] < pivot) i++;
            while (pivot < arr[j]) j--;
            if (i <= j) {
                (arr[i], arr[j]) = (arr[j], arr[i]);
                i++;
                j--;
            }
        }
        if (left < j)
            sort(arr, left, j);
        if (i < right)
            sort(arr, i, right);
    }
}