Submission e780e6d6...

ChallengeInteger sorting
Submittercottenio.eth
Submitted at2018-49-25
Gas used1019181
/**
 * 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 {
    /**
     * @dev Sorts a list of integers in ascending order.
     *
     * The input list may be of any length.
     * Simple QuickSort BENCHMARK
     * Adapted from: https://gist.github.com/subhodi/b3b86cc13ad2636420963e692a4d896f
     *
     * @param input The list of integers to sort.
     * @return The sorted list.
     */
    function sort(uint[] input) public pure returns(uint[]) {
        int n = int(input.length);

        if (n < 2) {
            return input;
        }

        quickSort(input, 0, n-1);

        return input;
    }

    function quickSort(uint[] memory arr, int left, int right) pure internal {
        int i = left;
        int j = right;

        uint pivot = arr[uint(left + (right - left) / 2)];

        while (i <= j) {
            while (arr[uint(i)] < pivot) i++;
            while (pivot < arr[uint(j)]) j--;
            if (i <= j) {
                (arr[uint(i)], arr[uint(j)]) = (arr[uint(j)], arr[uint(i)]);
                i++;
                j--;
            }
        }

        if (left < j)
            quickSort(arr, left, j);
        if (i < right)
            quickSort(arr, i, right);
    }
}