function A=merge(L,R)
  s=length(L);      % length of left  sequene L is s
  t=length(R);      % length of right sequence R is t
  S=1;              % Runs through L
  T=1;              % Runs through R
  A=zeros(1,s+t);   % Merging is not in-place

  for  P= 1:(s+t)   % Build output one element at a time
    if  S >  s                             % This is Case 3
      A(P) = R(T); T=T+1;       % copy R(T:t) to A...
    elseif  T > t                          % This is Case 4
           A(P) = L(S); S=S+1;  % copy L(S:s) to A
    elseif L(S) <= R(T)                    % This is Case 1
           A(P)  = L(S); S=S+1; % copy element
    else                                   % This is Case 2
           A(P)  = R(T); T=T+1; % copy element
    end
  end
end
