function [ d, v ] = svsort ( n, d, v )

%*****************************************************************************80
%
%% SVSORT descending sorts singular values D and adjusts V.
%
%  Discussion:
%
%    A simple bubble sort is used on D.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    30 July 2016
%
%  Author:
%
%    Original FORTRAN77 version by Richard Brent.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Richard Brent,
%    Algorithms for Minimization with Derivatives,
%    Prentice Hall, 1973,
%    Reprinted by Dover, 2002.
%
%  Parameters:
%
%    Input, integer N, the length of D, and the order of V.
%
%    Input/output, real D(N), the vector to be sorted.
%    On output, the entries of D are in descending order.
%
%    Input/output, real V(N,N), an N by N array to be adjusted
%    as D is sorted.  In particular, if the value that was in D(I) on input is
%    moved to D(J) on output, then the input column V(*,I) is moved to
%    the output column V(*,J).
%
  for j = 1 : n - 1

    j3 = j;
    for j2 = j + 1 : n
      if ( d(j3) < d(j2) )
        j3 = j2;
      end
    end

    t     = d(j);
    d(j)  = d(j3);
    d(j3) = t;

    for i = 1 : n
      t       = v(i,j);
      v(i,j)  = v(i,j3);
      v(i,j3) = t;
    end

  end

  return
end

