Logo Search packages:      
Sourcecode: yorick-imutil version File versions  Download package

insort.c

/*
 *  A library of sorting functions
 *
 *  Written by:  Ariel Faigon,  1987
 *
 * This program is free software; you can redistribute it and/or  modify it
 * under the terms of the GNU General Public License  as  published  by the
 * Free Software Foundation; either version 2 of the License,  or  (at your
 * option) any later version.
 *
 * This program is distributed in the hope  that  it  will  be  useful, but
 * WITHOUT  ANY   WARRANTY;   without   even   the   implied   warranty  of
 * MERCHANTABILITY or  FITNESS  FOR  A  PARTICULAR  PURPOSE.   See  the GNU
 * General Public License for more details (to receive a  copy  of  the GNU
 * General Public License, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA).
 *
 */
#include    <stdio.h>
#include    "sort.h"

/*------------------------------------------------------------------- 
 *            This file shouldn't be touched.
 *         For customizable parameters, see 'sort.h'
 *-----------------------------------------------------------------*/

/*
 |  void  insort (array, len)
 |  KEY_T  array[];
 |  int    len;
 |
 |  Abstract:     Sort array[0..len-1] into increasing order.
 |
 |  Method: Optimized insertion-sort (ala Jon Bentley)
 */

void  insort_long (long *array, int len)
{
      int   i, j;
      long  temp;

      for (i = 1; i < len; i++) {
            /* invariant:  array[0..i-1] is sorted */
            j = i;
            /* customization bug: SWAP is not used here */
            temp = array[j];
            while (j > 0 && GT(array[j-1], temp)) {
                  array[j] = array[j-1];
                  j--;
            }
            array[j] = temp;
      }
}

void  insort_float (array, len)
register float  array[];
register int    len;
{
      register int      i, j;
      register float    temp;

      for (i = 1; i < len; i++) {
            /* invariant:  array[0..i-1] is sorted */
            j = i;
            /* customization bug: SWAP is not used here */
            temp = array[j];
            while (j > 0 && GT(array[j-1], temp)) {
                  array[j] = array[j-1];
                  j--;
            }
            array[j] = temp;
      }
}

void  insort_double (array, len)
register double array[];
register int    len;
{
      register int      i, j;
      register double   temp;

      for (i = 1; i < len; i++) {
            /* invariant:  array[0..i-1] is sorted */
            j = i;
            /* customization bug: SWAP is not used here */
            temp = array[j];
            while (j > 0 && GT(array[j-1], temp)) {
                  array[j] = array[j-1];
                  j--;
            }
            array[j] = temp;
      }
}

void  insort_short (array, len)
register short  array[];
register int    len;
{
      register int      i, j;
      register short    temp;

      for (i = 1; i < len; i++) {
            /* invariant:  array[0..i-1] is sorted */
            j = i;
            /* customization bug: SWAP is not used here */
            temp = array[j];
            while (j > 0 && GT(array[j-1], temp)) {
                  array[j] = array[j-1];
                  j--;
            }
            array[j] = temp;
      }
}


Generated by  Doxygen 1.6.0   Back to index