myqsort.c

Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public License as
00007    published by the Free Software Foundation; either version 2 of the
00008    License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 /*
00021  * Standard qsort function modified to add a user data argument to 
00022  * the comparison function.
00023  */
00024 #ifdef HAVE_CONFIG_H
00025 #include "config.h"
00026 #endif /* HAVE_CONFIG_H */
00027 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "myqsort.h"
00032 
00033 /* Byte-wise swap two items of size SIZE. */
00034 #define SWAP(a, b, size)                                                      \
00035   do                                                                          \
00036     {                                                                         \
00037       register size_t __size = (size);                                        \
00038       register char *__a = (a), *__b = (b);                                   \
00039       do                                                                      \
00040         {                                                                     \
00041           char __tmp = *__a;                                                  \
00042           *__a++ = *__b;                                                      \
00043           *__b++ = __tmp;                                                     \
00044         } while (--__size > 0);                                               \
00045     } while (0)
00046 
00047 /* Discontinue quicksort algorithm when partition gets below this size.
00048    This particular magic number was chosen to work best on a Sun 4/260. */
00049 #define MAX_THRESH 4
00050 
00051 /* Stack node declarations used to store unfulfilled partition obligations. */
00052 typedef struct
00053   {
00054     char *lo;
00055     char *hi;
00056   } stack_node;
00057 
00058 /* The next 4 #defines implement a very fast in-line stack abstraction. */
00059 #define STACK_SIZE      (8 * sizeof(unsigned long int))
00060 #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
00061 #define POP(low, high)  ((void) (--top, (low = top->lo), (high = top->hi)))
00062 #define STACK_NOT_EMPTY (stack < top)
00063 
00064 
00065 /* Order size using quicksort.  This implementation incorporates
00066    four optimizations discussed in Sedgewick:
00067 
00068    1. Non-recursive, using an explicit stack of pointer that store the
00069       next array partition to sort.  To save time, this maximum amount
00070       of space required to store an array of MAX_INT is allocated on the
00071       stack.  Assuming a 32-bit integer, this needs only 32 *
00072       sizeof(stack_node) == 136 bits.  Pretty cheap, actually.
00073 
00074    2. Chose the pivot element using a median-of-three decision tree.
00075       This reduces the probability of selecting a bad pivot value and
00076       eliminates certain extraneous comparisons.
00077 
00078    3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
00079       insertion sort to order the MAX_THRESH items within each partition.
00080       This is a big win, since insertion sort is faster for small, mostly
00081       sorted array segments.
00082 
00083    4. The larger of the two sub-partitions is always pushed onto the
00084       stack first, with the algorithm then concentrating on the
00085       smaller partition.  This *guarantees* no more than log (n)
00086       stack size is needed (actually O(1) in this case)!  */
00087 
00088 void
00089 myqsort(void *const pbase, size_t total_elems, size_t size, myqsort_cmp cmp, void *data)
00090 {
00091   register char *base_ptr = (char *) pbase;
00092 
00093   /* Allocating SIZE bytes for a pivot buffer facilitates a better
00094      algorithm below since we can do comparisons directly on the pivot. */
00095   char *pivot_buffer = (char *) malloc (size);
00096   const size_t max_thresh = MAX_THRESH * size;
00097 
00098   if (total_elems == 0) {
00099     /* Avoid lossage with unsigned arithmetic below.  */
00100     free(pivot_buffer);
00101     return;
00102   }
00103 
00104   if (total_elems > MAX_THRESH)
00105     {
00106       char *lo = base_ptr;
00107       char *hi = &lo[size * (total_elems - 1)];
00108       /* Largest size needed for 32-bit int!!! */
00109       stack_node stack[STACK_SIZE];
00110       stack_node *top = stack + 1;
00111 
00112       while (STACK_NOT_EMPTY)
00113         {
00114           char *left_ptr;
00115           char *right_ptr;
00116 
00117           char *pivot = pivot_buffer;
00118 
00119           /* Select median value from among LO, MID, and HI. Rearrange
00120              LO and HI so the three values are sorted. This lowers the
00121              probability of picking a pathological pivot value and
00122              skips a comparison for both the LEFT_PTR and RIGHT_PTR. */
00123 
00124           char *mid = lo + size * ((hi - lo) / size >> 1);
00125 
00126           if ((*cmp) (data, (void *) mid, (void *) lo) < 0)
00127             SWAP (mid, lo, size);
00128           if ((*cmp) (data, (void *) hi, (void *) mid) < 0)
00129             SWAP (mid, hi, size);
00130           else
00131             goto jump_over;
00132           if ((*cmp) (data, (void *) mid, (void *) lo) < 0)
00133             SWAP (mid, lo, size);
00134         jump_over:;
00135           memcpy (pivot, mid, size);
00136           pivot = pivot_buffer;
00137 
00138           left_ptr  = lo + size;
00139           right_ptr = hi - size;
00140 
00141           /* Here's the famous ``collapse the walls'' section of quicksort.
00142              Gotta like those tight inner loops!  They are the main reason
00143              that this algorithm runs much faster than others. */
00144           do
00145             {
00146               while ((*cmp) (data, (void *) left_ptr, (void *) pivot) < 0)
00147                 left_ptr += size;
00148 
00149               while ((*cmp) (data, (void *) pivot, (void *) right_ptr) < 0)
00150                 right_ptr -= size;
00151 
00152               if (left_ptr < right_ptr)
00153                 {
00154                   SWAP (left_ptr, right_ptr, size);
00155                   left_ptr += size;
00156                   right_ptr -= size;
00157                 }
00158               else if (left_ptr == right_ptr)
00159                 {
00160                   left_ptr += size;
00161                   right_ptr -= size;
00162                   break;
00163                 }
00164             }
00165           while (left_ptr <= right_ptr);
00166 
00167           /* Set up pointers for next iteration.  First determine whether
00168              left and right partitions are below the threshold size.  If so,
00169              ignore one or both.  Otherwise, push the larger partition's
00170              bounds on the stack and continue sorting the smaller one. */
00171 
00172           if ((size_t) (right_ptr - lo) <= max_thresh)
00173             {
00174               if ((size_t) (hi - left_ptr) <= max_thresh)
00175                 /* Ignore both small partitions. */
00176                 POP (lo, hi);
00177               else
00178                 /* Ignore small left partition. */
00179                 lo = left_ptr;
00180             }
00181           else if ((size_t) (hi - left_ptr) <= max_thresh)
00182             /* Ignore small right partition. */
00183             hi = right_ptr;
00184           else if ((right_ptr - lo) > (hi - left_ptr))
00185             {
00186               /* Push larger left partition indices. */
00187               PUSH (lo, right_ptr);
00188               lo = left_ptr;
00189             }
00190           else
00191             {
00192               /* Push larger right partition indices. */
00193               PUSH (left_ptr, hi);
00194               hi = right_ptr;
00195             }
00196         }
00197     }
00198 
00199   /* Once the BASE_PTR array is partially sorted by quicksort the rest
00200      is completely sorted using insertion sort, since this is efficient
00201      for partitions below MAX_THRESH size. BASE_PTR points to the beginning
00202      of the array to sort, and END_PTR points at the very last element in
00203      the array (*not* one beyond it!). */
00204 
00205 #define min(x, y) ((x) < (y) ? (x) : (y))
00206 
00207   {
00208     char *const end_ptr = &base_ptr[size * (total_elems - 1)];
00209     char *tmp_ptr = base_ptr;
00210     char *thresh = min(end_ptr, base_ptr + max_thresh);
00211     register char *run_ptr;
00212 
00213     /* Find smallest element in first threshold and place it at the
00214        array's beginning.  This is the smallest array element,
00215        and the operation speeds up insertion sort's inner loop. */
00216 
00217     for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
00218       if ((*cmp) (data, (void *) run_ptr, (void *) tmp_ptr) < 0)
00219         tmp_ptr = run_ptr;
00220 
00221     if (tmp_ptr != base_ptr)
00222       SWAP (tmp_ptr, base_ptr, size);
00223 
00224     /* Insertion sort, running from left-hand-side up to right-hand-side.  */
00225 
00226     run_ptr = base_ptr + size;
00227     while ((run_ptr += size) <= end_ptr)
00228       {
00229         tmp_ptr = run_ptr - size;
00230         while ((*cmp) (data, (void *) run_ptr, (void *) tmp_ptr) < 0)
00231           tmp_ptr -= size;
00232 
00233         tmp_ptr += size;
00234         if (tmp_ptr != run_ptr)
00235           {
00236             char *trav;
00237 
00238             trav = run_ptr + size;
00239             while (--trav >= run_ptr)
00240               {
00241                 char c = *trav;
00242                 char *hi, *lo;
00243 
00244                 for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
00245                   *hi = *lo;
00246                 *hi = c;
00247               }
00248           }
00249       }
00250   }
00251 
00252   free(pivot_buffer);
00253 }

Generated on Sun Jun 8 10:56:34 2008 for GNUmifluz by  doxygen 1.5.5