function [ n, x, indx ] = i4vec_index_insert ( n, x, indx, xval )

%*****************************************************************************80
%
%% i4vec_index_insert() inserts a value in an indexed sorted I4VEC.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    04 November 2000
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the size of the current list.
%
%    integer X(N), the list.
%
%    integer INDX(N), the sort index of the list.
%
%    integer XVAL, the value to be sought.
%
%  Output:
%
%    integer N, the size of the current list.
%
%    integer X(N), the list.
%
%    integer INDX(N), the sort index of the list.
%
  if ( n <= 0 )
    n = 1;
    x = [ xval ];
    indx = [ 1 ];
    return
  end

  [ less, equal, more ] = i4vec_index_search ( n, x, indx, xval );

  x(n+1) = xval;
  indx(n+1:-1:more+1) = indx(n:-1:more);
  indx(more) = n + 1;
  n = n + 1;

  return
end
