dbl_eg_numutil.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 #include "dbl_eg_numutil.h"
00021 /** @file
00022  * @ingroup EGlpNumUtil */
00023 /** @addtogroup EGlpNumUtil */
00024 /** @{ */
00025 /* ========================================================================= */
00026 void dbl_EGutilPermSort (const size_t sz,
00027                      int *const perm,
00028                      double * const elem)
00029 {
00030   size_t i,
00031     j,
00032     temp;
00033   double t;
00034   if (sz <= 1)
00035     return;
00036 
00037   dbl_EGlpNumInitVar (t);
00038   EGswap (perm[0], perm[(sz - 1) / 2], temp);
00039   i = 0;
00040   j = sz;
00041   dbl_EGlpNumCopy (t, elem[perm[0]]);
00042   for (;;)
00043   {
00044     do
00045       i++;
00046     while (i < sz && dbl_EGlpNumIsLess (elem[perm[i]], t));
00047     do
00048       j--;
00049     while (dbl_EGlpNumIsLess (t, elem[perm[j]]));
00050     if (j < i)
00051       break;
00052     EGswap (perm[i], perm[j], temp);
00053   }
00054   EGswap (perm[0], perm[j], temp);
00055   dbl_EGlpNumClearVar (t);
00056   dbl_EGutilPermSort (j, perm, elem);
00057   dbl_EGutilPermSort (sz - i, perm + i, elem);
00058 }
00059 
00060 /* ========================================================================= */
00061 void dbl_EGutilPermSort2 (const size_t sz,
00062                      int *const perm,
00063                      double * const elem)
00064 {
00065   size_t i,
00066     j,
00067     temp;
00068   double t;
00069   if (sz <= 1)
00070     return;
00071 
00072   dbl_EGlpNumInitVar (t);
00073   EGswap (perm[0], perm[(sz - 1) / 2], temp);
00074   i = 0;
00075   j = sz;
00076   dbl_EGlpNumCopy (t, elem[perm[0]]);
00077   for (;;)
00078   {
00079     do
00080       i++;
00081     while (i < sz && dbl_EGlpNumIsLess (t, elem[perm[i]]));
00082     do
00083       j--;
00084     while (dbl_EGlpNumIsLess (elem[perm[j]], t));
00085     if (j < i)
00086       break;
00087     EGswap (perm[i], perm[j], temp);
00088   }
00089   EGswap (perm[0], perm[j], temp);
00090   dbl_EGlpNumClearVar (t);
00091   dbl_EGutilPermSort2 (j, perm, elem);
00092   dbl_EGutilPermSort2 (sz - i, perm + i, elem);
00093 }
00094 
00095 /* ========================================================================= */
00096 /** @} */

Generated on Wed Nov 21 09:38:13 2007 for MTgomory by  doxygen 1.4.6