# Submission 52d0bfa4...

Challenge Integer sorting wicketh.eth 2018-42-02 387058
``````pragma solidity 0.4.24;

contract Sort {

function () external payable { assembly {

// @author Remco Bloemen <[email protected]>

// Copy input to memory
calldatacopy(0, 4, calldatasize)

// Special case for zero or one value
jumpi(done, lt(calldatasize, 0x84))

// Recursively sort
sort(0x40, sub(calldatasize, 0x24))
function sort(lo, hi) {

let lolo := lo
let hihi := hi
let d := sub(hi, lo)

if lt(d, 96) {

// Optimize for two
if eq(d, 32) {
{
if lt(b, a) {
mstore(lo, b)
mstore(hi, a)
}
}
jump(ret)
}

// Optimize for three
//if eq(sub(hi, lo), 64) {
{
if lt(b, a) {
a
b
=: a
=: b
}
if lt(c, b) {
b
c
=: b
=: c
if lt(b, a) {
a
b
=: a
=: b
}
}
mstore(lo, a)
mstore(hi, c)
}
jump(ret)
//}
}

// Partition
{
let pivot
let lov
let hiv

// Compute pivot value
0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0
)

loop:
for {} lt(lov, pivot) {} {
}
for {} gt(hiv, pivot) {} {
hi := sub(hi, 32)
}
jumpi(end, iszero(lt(lo, hi)))
mstore(lo, hiv)
mstore(hi, lov)
hi := sub(hi, 32)
jump(loop)

end:
lo := hi
}

// Recurse
if lt(lolo, lo) {
sort(lolo, lo)
}
if lt(hi, hihi) {
sort(hi, hihi)
}

ret:
}

done:
return(0, sub(calldatasize, 4))
}}
}

``````