home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LL_QSORT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  97 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. #include    <stdlib.h>
  4. #include    "snipsort.h"
  5.  
  6.  
  7. /*
  8.     This is a quicksort routine to be used to sort linked-lists
  9.     by Jon Guthrie.
  10. */
  11.  
  12. void    *sortl(void *list, void *(*getnext)(void *),
  13.             void (*setnext)(void *, void *), 
  14.             int (*compare)(void *, void *))
  15. {
  16. void    *low_list, *high_list, *current, *pivot, *temp;
  17. int     result;
  18.  
  19.     /*
  20.         Test for empty list.
  21.     */
  22.     if(NULL == list)
  23.         return(NULL);
  24.  
  25.     /*
  26.         Find the first element that doesn't have the same value as the first
  27.         element.
  28.     */
  29.     current = list;
  30.     do
  31.     {
  32.         current = getnext(current);
  33.         if(NULL == current)
  34.             return(list);
  35.     }   while(0 == (result = compare(list, current)));
  36.  
  37.     /*
  38.         My pivot value is the lower of the two.  This insures that the sort
  39.         will always terminate by guaranteeing that there will be at least one
  40.         member of both of the sublists.
  41.     */
  42.     if(result > 0)
  43.         pivot = current;
  44.     else
  45.         pivot = list;
  46.  
  47.     /* Initialize the sublist pointers */
  48.     low_list = high_list = NULL;
  49.  
  50.     /*
  51.         Now, separate the items into the two sublists
  52.     */
  53.     current = list;
  54.     while(NULL != current)
  55.     {
  56.         temp = getnext(current);
  57.         if(compare(pivot, current) < 0)
  58.         {
  59.             /* add one to the high list */
  60.             setnext(current, high_list);
  61.             high_list = current;
  62.         }
  63.         else
  64.         {
  65.             /* add one to the low list */
  66.             setnext(current, low_list);
  67.             low_list = current;
  68.         }
  69.         current = temp;
  70.     }
  71.  
  72.     /*
  73.         And, recursively call the sort for each of the two sublists.
  74.     */
  75.     low_list  = sortl(low_list, getnext, setnext, compare);
  76.     high_list = sortl(high_list, getnext, setnext, compare);
  77.  
  78.     /*
  79.         Now, I have to put the "high" list after the end of the "low" list.  
  80.         To do that, I first have to find the end of the "low" list...
  81.     */
  82.     current = temp = low_list;
  83.     while(1)
  84.     {
  85.         current = getnext(current);
  86.         if(NULL == current)
  87.             break;
  88.         temp = current;
  89.     }
  90.  
  91.     /*
  92.         Then, I put the "high" list at the end of the low list
  93.     */
  94.     setnext(temp, high_list);
  95.     return(low_list);
  96. }
  97.