/***********************************************
 (C) Copyright 1992-1993; dit/upm
   Distributed under the conditions stated in the
   EPS General Public License (see file LICENSE)
 ***********************************************
 $Log: auxIT.c,v $
 * Revision 2.3  1993/01/12  16:34:53  eps
 * portability issues
 *
 * Revision 2.2  1992/01/14  15:43:23  eps
 * distribution issues
 *
 * Revision 2.1  90/10/30  08:34:53  eps
 * hardwired colours added
 * new colours I2, IAT, AT
 * ! is no longer used as colour delimiter
 * still some bugs are known w.r.t. colour deallocation
 * 
 **********************************************/

#ifndef lint
static char rcsid[]= "$Id: auxIT.c,v 2.3 1993/01/12 16:34:53 eps Exp $";
#endif

/***********************************************************************
	Jose A. Manas
        dpt. Ingenieria Telematica
        E.T.S.I. Telecomunicacion
        Ciudad Universitaria
        E-28040  MADRID
        jmanas@dit.upm.es

        Tables of Integers: Auxiliary functions
 **********************************************************************/

/* LINTLIBRARY */

# define cast_IMP
# include "cast.hh"

PUBLIC IT*
ITsortP (tbl)
  IT* tbl;
{
  int i, j, m;
  int t;
  IT* perm;

  perm= ITcreate (tbl->size, -1, 0);
  for (i= 0; i < tbl->size; i++)
    perm->data[i]= i;
  perm->size= tbl->size;

  for (i= 0; i < tbl->size; i++) {
    m= i;
    for (j= i+1; j < tbl->size; j++)
      if (tbl->data[j] < tbl->data[m])
	m= j;
    if (m != i) {
      t= tbl->data[i];
      tbl->data[i]= tbl->data[m];
      tbl->data[m]= t;
      j= perm->data[i];
      perm->data[i]= perm->data[m];
      perm->data[m]= j;
    }
  }
  tbl->class= 2;
  return perm;
}
