% Iterative sorting by merging:  imerge or
%                           sort_nmerge
function A=sort_nmerge(A)
  n=length(A);
  s=1;
  while (s < n)
      for b=1:2*s:n
        if (b+2*s-1)<= n; rr= b+2*s-1; % second = b+s .. rr
          else  rr=n;
        end
        if (b+s-1)  <= n; ll=b+s-1;    % first  = b   .. ll
          else  ll=n;
        end
        A(b:rr) = merge(A(b:ll),A(ll+1:rr));
      end
      s= s*2;
  end
end
