Finish 2012-11-07 16:00:00 UTC

simple sort

by Werner

Status: Passed
Results: 134693 (cyc: 2, node: 201)
CPU Time: 0.193
Score: 134697.0
Submitted at: 2012-11-01 15:45:34 UTC
Scored at: 2012-11-01 20:55:45 UTC

Current Rank: 1697th (Highest: 51st )

Comments
Please login or create a profile.
Code
function xyOut = solver02(a, xyIn, wts)
    
    nr = size(a, 1);
    asum=sum(a, 1);
    norder = zeros(1, nr); 
    
    [~, ix] = min(asum);
    norder(1) = ix;
    asum(ix) = 10000000;
    
    for i=2:nr
        [~, ix] = min(asum+10000 .* ~a(norder(i-1),:));
        norder(i) = ix;
        asum(ix) = 10000000;
    end
    
    xyOut(norder, :) = makecycle(xyIn);
    
end 




function ret = makecycle(ps)
    npoints = size(ps, 1);
    
    c = round(mean(ps, 1));
    r = ceil(npoints);
    
    is = 2*pi*(0:(npoints-1))/npoints;
    ret = out(r*[cos(is)', sin(is)']);
    
    ret = bsxfun(@plus, ret, c);
end

function m = out(m)
    pos = m > 0;
    
    m( pos) = ceil (m( pos));
    m(~pos) = floor(m(~pos));
end