slasrt.c
Go to the documentation of this file.
00001 /* slasrt.f -- translated by f2c (version 20061008).
00002    You must link the resulting object file with libf2c:
00003         on Microsoft Windows system, link with libf2c.lib;
00004         on Linux or Unix systems, link with .../path/to/libf2c.a -lm
00005         or, if you install libf2c.a in a standard place, with -lf2c -lm
00006         -- in that order, at the end of the command line, as in
00007                 cc *.o -lf2c -lm
00008         Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
00009 
00010                 http://www.netlib.org/f2c/libf2c.zip
00011 */
00012 
00013 #include "f2c.h"
00014 #include "blaswrap.h"
00015 
00016 /* Subroutine */ int slasrt_(char *id, integer *n, real *d__, integer *info)
00017 {
00018     /* System generated locals */
00019     integer i__1, i__2;
00020 
00021     /* Local variables */
00022     integer i__, j;
00023     real d1, d2, d3;
00024     integer dir;
00025     real tmp;
00026     integer endd;
00027     extern logical lsame_(char *, char *);
00028     integer stack[64]   /* was [2][32] */;
00029     real dmnmx;
00030     integer start;
00031     extern /* Subroutine */ int xerbla_(char *, integer *);
00032     integer stkpnt;
00033 
00034 
00035 /*  -- LAPACK routine (version 3.2) -- */
00036 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
00037 /*     November 2006 */
00038 
00039 /*     .. Scalar Arguments .. */
00040 /*     .. */
00041 /*     .. Array Arguments .. */
00042 /*     .. */
00043 
00044 /*  Purpose */
00045 /*  ======= */
00046 
00047 /*  Sort the numbers in D in increasing order (if ID = 'I') or */
00048 /*  in decreasing order (if ID = 'D' ). */
00049 
00050 /*  Use Quick Sort, reverting to Insertion sort on arrays of */
00051 /*  size <= 20. Dimension of STACK limits N to about 2**32. */
00052 
00053 /*  Arguments */
00054 /*  ========= */
00055 
00056 /*  ID      (input) CHARACTER*1 */
00057 /*          = 'I': sort D in increasing order; */
00058 /*          = 'D': sort D in decreasing order. */
00059 
00060 /*  N       (input) INTEGER */
00061 /*          The length of the array D. */
00062 
00063 /*  D       (input/output) REAL array, dimension (N) */
00064 /*          On entry, the array to be sorted. */
00065 /*          On exit, D has been sorted into increasing order */
00066 /*          (D(1) <= ... <= D(N) ) or into decreasing order */
00067 /*          (D(1) >= ... >= D(N) ), depending on ID. */
00068 
00069 /*  INFO    (output) INTEGER */
00070 /*          = 0:  successful exit */
00071 /*          < 0:  if INFO = -i, the i-th argument had an illegal value */
00072 
00073 /*  ===================================================================== */
00074 
00075 /*     .. Parameters .. */
00076 /*     .. */
00077 /*     .. Local Scalars .. */
00078 /*     .. */
00079 /*     .. Local Arrays .. */
00080 /*     .. */
00081 /*     .. External Functions .. */
00082 /*     .. */
00083 /*     .. External Subroutines .. */
00084 /*     .. */
00085 /*     .. Executable Statements .. */
00086 
00087 /*     Test the input paramters. */
00088 
00089     /* Parameter adjustments */
00090     --d__;
00091 
00092     /* Function Body */
00093     *info = 0;
00094     dir = -1;
00095     if (lsame_(id, "D")) {
00096         dir = 0;
00097     } else if (lsame_(id, "I")) {
00098         dir = 1;
00099     }
00100     if (dir == -1) {
00101         *info = -1;
00102     } else if (*n < 0) {
00103         *info = -2;
00104     }
00105     if (*info != 0) {
00106         i__1 = -(*info);
00107         xerbla_("SLASRT", &i__1);
00108         return 0;
00109     }
00110 
00111 /*     Quick return if possible */
00112 
00113     if (*n <= 1) {
00114         return 0;
00115     }
00116 
00117     stkpnt = 1;
00118     stack[0] = 1;
00119     stack[1] = *n;
00120 L10:
00121     start = stack[(stkpnt << 1) - 2];
00122     endd = stack[(stkpnt << 1) - 1];
00123     --stkpnt;
00124     if (endd - start <= 20 && endd - start > 0) {
00125 
00126 /*        Do Insertion sort on D( START:ENDD ) */
00127 
00128         if (dir == 0) {
00129 
00130 /*           Sort into decreasing order */
00131 
00132             i__1 = endd;
00133             for (i__ = start + 1; i__ <= i__1; ++i__) {
00134                 i__2 = start + 1;
00135                 for (j = i__; j >= i__2; --j) {
00136                     if (d__[j] > d__[j - 1]) {
00137                         dmnmx = d__[j];
00138                         d__[j] = d__[j - 1];
00139                         d__[j - 1] = dmnmx;
00140                     } else {
00141                         goto L30;
00142                     }
00143 /* L20: */
00144                 }
00145 L30:
00146                 ;
00147             }
00148 
00149         } else {
00150 
00151 /*           Sort into increasing order */
00152 
00153             i__1 = endd;
00154             for (i__ = start + 1; i__ <= i__1; ++i__) {
00155                 i__2 = start + 1;
00156                 for (j = i__; j >= i__2; --j) {
00157                     if (d__[j] < d__[j - 1]) {
00158                         dmnmx = d__[j];
00159                         d__[j] = d__[j - 1];
00160                         d__[j - 1] = dmnmx;
00161                     } else {
00162                         goto L50;
00163                     }
00164 /* L40: */
00165                 }
00166 L50:
00167                 ;
00168             }
00169 
00170         }
00171 
00172     } else if (endd - start > 20) {
00173 
00174 /*        Partition D( START:ENDD ) and stack parts, largest one first */
00175 
00176 /*        Choose partition entry as median of 3 */
00177 
00178         d1 = d__[start];
00179         d2 = d__[endd];
00180         i__ = (start + endd) / 2;
00181         d3 = d__[i__];
00182         if (d1 < d2) {
00183             if (d3 < d1) {
00184                 dmnmx = d1;
00185             } else if (d3 < d2) {
00186                 dmnmx = d3;
00187             } else {
00188                 dmnmx = d2;
00189             }
00190         } else {
00191             if (d3 < d2) {
00192                 dmnmx = d2;
00193             } else if (d3 < d1) {
00194                 dmnmx = d3;
00195             } else {
00196                 dmnmx = d1;
00197             }
00198         }
00199 
00200         if (dir == 0) {
00201 
00202 /*           Sort into decreasing order */
00203 
00204             i__ = start - 1;
00205             j = endd + 1;
00206 L60:
00207 L70:
00208             --j;
00209             if (d__[j] < dmnmx) {
00210                 goto L70;
00211             }
00212 L80:
00213             ++i__;
00214             if (d__[i__] > dmnmx) {
00215                 goto L80;
00216             }
00217             if (i__ < j) {
00218                 tmp = d__[i__];
00219                 d__[i__] = d__[j];
00220                 d__[j] = tmp;
00221                 goto L60;
00222             }
00223             if (j - start > endd - j - 1) {
00224                 ++stkpnt;
00225                 stack[(stkpnt << 1) - 2] = start;
00226                 stack[(stkpnt << 1) - 1] = j;
00227                 ++stkpnt;
00228                 stack[(stkpnt << 1) - 2] = j + 1;
00229                 stack[(stkpnt << 1) - 1] = endd;
00230             } else {
00231                 ++stkpnt;
00232                 stack[(stkpnt << 1) - 2] = j + 1;
00233                 stack[(stkpnt << 1) - 1] = endd;
00234                 ++stkpnt;
00235                 stack[(stkpnt << 1) - 2] = start;
00236                 stack[(stkpnt << 1) - 1] = j;
00237             }
00238         } else {
00239 
00240 /*           Sort into increasing order */
00241 
00242             i__ = start - 1;
00243             j = endd + 1;
00244 L90:
00245 L100:
00246             --j;
00247             if (d__[j] > dmnmx) {
00248                 goto L100;
00249             }
00250 L110:
00251             ++i__;
00252             if (d__[i__] < dmnmx) {
00253                 goto L110;
00254             }
00255             if (i__ < j) {
00256                 tmp = d__[i__];
00257                 d__[i__] = d__[j];
00258                 d__[j] = tmp;
00259                 goto L90;
00260             }
00261             if (j - start > endd - j - 1) {
00262                 ++stkpnt;
00263                 stack[(stkpnt << 1) - 2] = start;
00264                 stack[(stkpnt << 1) - 1] = j;
00265                 ++stkpnt;
00266                 stack[(stkpnt << 1) - 2] = j + 1;
00267                 stack[(stkpnt << 1) - 1] = endd;
00268             } else {
00269                 ++stkpnt;
00270                 stack[(stkpnt << 1) - 2] = j + 1;
00271                 stack[(stkpnt << 1) - 1] = endd;
00272                 ++stkpnt;
00273                 stack[(stkpnt << 1) - 2] = start;
00274                 stack[(stkpnt << 1) - 1] = j;
00275             }
00276         }
00277     }
00278     if (stkpnt > 0) {
00279         goto L10;
00280     }
00281     return 0;
00282 
00283 /*     End of SLASRT */
00284 
00285 } /* slasrt_ */


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:56:11