C QSPLIT    SOURCE    CHAT      05/01/13    02:40:39     5004
      SUBROUTINE QSPLIT(TABLR,TABLI,NTABL,NCUT,
     $     IMPR,IRET)
      IMPLICIT INTEGER(I-N)
      IMPLICIT REAL*8 (A-H,O-Z)
C***********************************************************************
C NOM         : QSPLIT
C DESCRIPTION :
C Quicksplit : renvoie les NCUT plus grandes valeurs d'un tableau de
C              dimension NTABL.
C TABLR et TABLI sont des arguments d'entrée-sortie
C En sortie : TABLR est modifié de telle façon que TABLR(1:NCUT)
C             contient les NCUT plus grands éléments de TABLR.
C             TABLI est un tableau de même dimenson que TABLR où
C             l'on effectue les mêmes permutations d'éléments que dans
C             TABL.
C
C LANGAGE     : ESOPE
C AUTEUR      : Stéphane GOUNAND (CEA/DRN/DMT/SEMT/LTMF)
C               mél : gounand@semt2.smts.cea.fr
C REFERENCE (bibtex-like) :
C  Sparskit : a basic tool kit for sparse matrix computations
C  Version 2 (Youcef Saad)
C  -> URL : http://www.cs.umn.edu/Research/arpa/SPARSKIT/sparskit.html
C REMARQUES :
C Une autre façon (vraisemblablement plus rapide pour les grands NTABL)
C serait d'employer l'algorithme d'Alexeev (Knuth, Art of Programming
C Vol.3  2nd Ed p 232)
C
C***********************************************************************
C APPELES          : -
C APPELE PAR       : MEILUT
C***********************************************************************
C ENTREES            : NTABL, NCUT
C ENTREES/SORTIES    : TABLR,TABLI
C CODE RETOUR (IRET) : = 0 si tout s'est bien passé
C***********************************************************************
C VERSION    : v1, 23/02/2000, version initiale
C HISTORIQUE : v1, 23/02/2000, création
C HISTORIQUE :
C HISTORIQUE :
C***********************************************************************
C Prière de PRENDRE LE TEMPS de compléter les commentaires
C en cas de modification de ce sous-programme afin de faciliter
C la maintenance !
C***********************************************************************
-INC PPARAM
-INC CCOPTIO
*
      INTEGER NTABL,NCUT
      REAL*8 TABLR(NTABL)
      INTEGER TABLI(NTABL)

      INTEGER IMPR,IRET
*
      INTEGER IDX,ISTRT,ISTOP,IMID
      INTEGER ITMP
      REAL*8 ABSKEY,RTMP
*
* Executable statements
*
      IF (IMPR.GT.5) WRITE(IOIMP,*) 'Entrée dans qsplit.eso'
      ISTRT=1
      ISTOP=NTABL
*      IF (NCUT.GE.ISTRT.AND.NCUT.LE.ISTOP) THEN
      IF (NCUT.GE.ISTRT.AND.NCUT.LT.ISTOP) THEN
c     outer loop -- while mid .ne. ncut do
 1       CONTINUE
         IMID=ISTRT
         ABSKEY=ABS(TABLR(IMID))
         DO 12 IDX=ISTRT+1,ISTOP
            IF (ABS(TABLR(IDX)).GT.ABSKEY) THEN
               IMID=IMID+1
c     interchange
               RTMP=TABLR(IMID)
               ITMP=TABLI(IMID)
               TABLR(IMID)=TABLR(IDX)
               TABLI(IMID)=TABLI(IDX)
               TABLR(IDX)=RTMP
               TABLI(IDX)=ITMP
            ENDIF
 12      CONTINUE
c     interchange
         RTMP=TABLR(IMID)
         ITMP=TABLI(IMID)
         TABLR(IMID)=TABLR(ISTRT)
         TABLI(IMID)=TABLI(ISTRT)
         TABLR(ISTRT)=RTMP
         TABLI(ISTRT)=ITMP
c     test for while loop
         IF (IMID.NE.NCUT) THEN
            IF (IMID.GT.NCUT) THEN
               ISTOP=IMID-1
            ELSE
               ISTRT=IMID+1
            ENDIF
            GOTO 1
         ENDIF
      ENDIF
*
* Teste le bon fonctionnement
*
*!!      DO IFIN=NCUT+1,NTABL
*!!         XCOMP1=ABS(TABLR(IFIN))
*!!         DO IDEB=1,NCUT
*!!           XCOMP2=ABS(TABLR(IDEB))
*!!           IF (XCOMP1.GT.XCOMP2) THEN
*!!              WRITE(IOIMP,*) 'Erreur de programmation dans qsplit'
*!!              GOTO 9999
*!!           ENDIF
*!!         ENDDO
*!!      ENDDO
*
* Normal termination
*
      IRET=0
      RETURN
*
* Format handling
*
*
* Error handling
*
 9999 CONTINUE
      IRET=1
      WRITE(IOIMP,*) 'An error was detected in subroutine qsplit'
      RETURN
*
* End of subroutine QSPLIT
*
      END




