home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 191.lha / ARequester / qsort.c < prev    next >
Text File  |  1988-04-28  |  721b  |  53 lines

  1. #define ULONG unsigned long
  2.  
  3.  
  4. static short (*_comp)();
  5. static void  (*_swap)();
  6.  
  7. static ULONG _rearr(lb,ub)
  8. register ULONG lb, ub;
  9. {
  10.  
  11. do {
  12.    while( ub > lb && (*_comp)(ub, lb) >= 0)
  13.       ub--;
  14.    if( ub != lb )
  15.       {
  16.       (*_swap)(ub,lb);
  17.       while(lb < ub && (*_comp)(lb,ub) <= 0)
  18.          lb++;
  19.       if( lb != ub )
  20.          (*_swap)(lb, ub);
  21.       }
  22.    } while( lb != ub );
  23.  
  24. return(lb);
  25. }
  26.  
  27.  
  28. static void _quick(lb, ub)
  29. register ULONG lb, ub;
  30. {
  31. register ULONG j;
  32.  
  33. if( lb < ub )
  34.    {
  35.    if( j = _rearr(lb, ub))
  36.       _quick(lb,j - 1L);
  37.    _quick(j + 1L, ub);
  38.    }
  39. }
  40.  
  41.  
  42. void quicksort(n,comp,swap)
  43. register ULONG n;
  44. short (*comp)();
  45. void (*swap)();
  46. {
  47. _comp = comp;
  48. _swap = swap;
  49. _quick((ULONG)0,(ULONG)(n - 1));
  50. }
  51.  
  52.  
  53.