function sort_heap_external_test ( )

%*****************************************************************************80
%
%% sort_heap_external_test() tests sort_heap_external().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 September 2018
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'SORT_HEAP_EXTERNAL_TEST():\n' );
  fprintf ( 1, '  SORT_HEAP_EXTERNAL sorts objects externally.\n' );

  n = 20;
  a = [ 5, 20, 17, 12, 9, 2, 6, 3, 1, 13, 2, 9, 9, 16, 16, 1, 18, 8, 2, 1 ];

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Unsorted array:\n' );
  fprintf ( 1, '\n' );
  for i = 1 : n
    fprintf ( 1, '  %2d  %2d\n', i, a(i) );
  end

  indx = 0;
  isgn = 0;
  i1 = 0;
  j1 = 0;
  k0 = 0;
  k1 = 0;
  n1 = 0;

  while ( true )

    [ indx, i, j ] = sort_heap_external ( n, indx, isgn );

    if ( indx < 0 )
      isgn = 1;
      if ( a(i) <= a(j) )
        isgn = -1;
      end 
    elseif ( 0 < indx )
      t = a(i);
      a(i) = a(j);
      a(j) = t;
    else
      break;
    end

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Sorted array:\n' );
  fprintf ( 1, '\n' );
  for i = 1 : n
    fprintf ( 1, '  %2d  %2d\n', i, a(i) );
  end

  return
end
