# Submission 3eef71c1...

Challenge Integer sorting 0x1dba1131000664b... 2018-58-26 592766
``````/**
* This file is part of the 1st Solidity Gas Golfing Contest.
*
* Author: Jordi Baylina
*/

pragma solidity 0.4.24;

contract Sort {
/**
* @dev Sorts a list of integers in ascending order.
*
* The input list may be of any length.
*
* @param input The list of integers to sort.
* @return The sorted list.
*/

struct List {
uint start;
uint end;
}
function sort(uint[]  input) public pure returns(uint[] aux) {
uint il = input.length;
if (il<2) return input;
aux = new uint[](input.length);

uint[] memory listEnd = new uint[](input.length);
uint[] memory src = input;
uint[] memory dst = aux;

uint nlists = 0;

assembly {

/*
uint last = input[0];
for (uint i=1; i<il; i++) {
uint act = input[i];
if (last > act) {
listEnd[nlists++] = i;
}
last = act;
}
listEnd[nlists++] = il;
*/

for {
let i := 1
} lt(i,il) {
}
{
if lt(act, last) {
mstore(ple, i)
}
last := act
}
mstore(ple, il)

/*
while (nlists>1) {
for (uint l=0; l<nlists; l+=2) {
if (l+1 == nlists) {
copyList(src, dst, listEnd[l-1], il);
listEnd[l/2] = il;
} else {
joinList(src, dst, l==0 ? 0 : listEnd[l-1], listEnd[l], listEnd[l+1]);
listEnd[l/2] = listEnd[l+1];
}
}

nlists = (nlists+1)/2;

(src, dst) = (dst, src);
}
*/

for {} gt(nlists, 1) { } {
for {
let l:=0
} lt(l, nlists) {
} {
case 1 {
copyList(src, dst, le_min1, il)
mstore(outp, il)
}
default {
if eq(l,0) {
le_min1 := 0
}
joinList(src, dst, le_min1, le_0, le_plus1)
mstore(outp, le_plus1)
}
}

let aux := src
src := dst
dst := aux
}

/*
function copyList(uint[] memory src, uint[] memory dst, uint p1, uint e1) pure internal {

uint o=p1;
while (p1<e1) dst[o++] = src[p1++];
}
*/

function copyList (src, dst, p1, e1) {
for {
let ofs := add(32, mul(p1, 32) )
} lt (inp, end) {
} {
}
}

/*
function joinList(uint[] memory src, uint[] memory dst, uint p1, uint p2, uint e2) pure internal {
uint e1 = p2;
uint o=p1;

while ((p2<e2)&&(p1<e1)) {
dst[o++] = (src[p1]<src[p2]) ? src[p1++] : src[p2++];
}
while (p1<e1) dst[o++] = src[p1++];
while (p2<e2) dst[o++] = src[p2++];
}
*/
function joinList(src, dst, p1, p2, e2) {
let ine1 := inp2
for {
} and(lt(inp1, ine1), lt(inp2, ine2))
{
} {
switch lt(a,b)
case 0 {
mstore(out,b)
}
default {
mstore(out,a)
}
}
for {} lt(inp1, ine1) {
} {
}
for {} lt(inp2, ine2) {
} {