home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / lib / objcol2 / src / sort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-11  |  2.6 KB  |  142 lines

  1. #include <stdio.h>
  2.  
  3. #define    BYTE(x)        *((unsigned char *)(x))
  4. #define    WORD(x)        *((unsigned short int *)(x))
  5. #define    DWORD(x)    *((unsigned long int *)(x))
  6. #define POINT(x)    *((char **)(x))
  7.  
  8. #define    SGN(x)        ( (x>0)? 1: ((x<0)? -1: x) )
  9.  
  10. #define SORT_BUBBLE    1
  11. #define SORT_SHELL    2
  12.  
  13. #define    SORT_CHAR    1
  14. #define    SORT_SHORT    2
  15. #define    SORT_LONG    3
  16. #define    SORT_STR    4
  17.  
  18. #define    CHAR_SIZE    sizeof(char)
  19. #define    SHORT_SIZE    sizeof(short)
  20. #define    LONG_SIZE    sizeof(long)
  21. #define    POINT_SIZE    sizeof(char *)
  22.  
  23. #define    NOEXCHANGE    0
  24. #define    EXCHANGE    1
  25.  
  26. static int    type;
  27. static int    *lank;
  28. static char    *base;
  29. static int    element;
  30.  
  31. static int    compare( int offset1, int offset2 );
  32. static void    shellswap( int length, int offset );
  33. static void    shellsort();
  34. static void    bubblesort();
  35.  
  36. static int compare( int offset1, int offset2 )
  37. {
  38.     long int    i,j;
  39.  
  40.     switch( type )
  41.     {
  42.         case SORT_CHAR:
  43.             i = BYTE( base + CHAR_SIZE * offset1 )
  44.                 - BYTE( base + CHAR_SIZE * offset2 );
  45.             break;
  46.         case SORT_SHORT:
  47.             i = WORD( base + SHORT_SIZE * offset1 )
  48.                 - WORD( base + SHORT_SIZE * offset2 );
  49.             break;
  50.         case SORT_LONG:
  51.             i = DWORD( base + LONG_SIZE * offset1 )
  52.                 - DWORD( base + LONG_SIZE * offset2 );
  53.             break;
  54.         case SORT_STR:
  55.             j = 0;
  56.             while(1)
  57.             {
  58.                 i = BYTE( POINT( base + POINT_SIZE * offset1 ) + CHAR_SIZE * j )
  59.                     - BYTE( POINT( base + POINT_SIZE * offset2 ) + CHAR_SIZE * j );
  60.                 if( i )
  61.                     break;
  62.                 if( BYTE( POINT( base + POINT_SIZE * offset1 ) + CHAR_SIZE * j ) == 0 )
  63.                     break;
  64.                 ++ j;
  65.             }
  66.             break;
  67.     }
  68.  
  69.     return SGN(i);
  70. }
  71.  
  72. static void shellswap( int length, int offset )
  73. {
  74.     if( compare( lank[offset], lank[offset+length] ) > 0 )
  75.     {
  76.         int        i;
  77.         i = lank[offset];
  78.         lank[offset] = lank[offset+length];
  79.         lank[offset+length] = i;
  80.         if( offset < length )
  81.             return;
  82.         shellswap( length, offset-length );
  83.     }
  84.     return;
  85. }
  86.  
  87. static void shellsort()
  88. {
  89.     int        i,length;
  90.  
  91.     length = element;
  92.     while( length>=1 )
  93.     {
  94.         length /= 2;
  95.         for( i=0; i+length<element; i++ )
  96.             shellswap( length, i );
  97.     }
  98.  
  99.     return;
  100. }
  101.  
  102. static void bubblesort()
  103. {
  104.     int        i,j,k;
  105.  
  106.     for( i=0; i<element; i++ )
  107.         for( j=i+1; j<element; j++ )
  108.             if( compare( lank[i], lank[j] ) > 0 )
  109.             {
  110.                 k = lank[i];
  111.                 lank[i] = lank[j];
  112.                 lank[j] = k;
  113.             }
  114.  
  115.     return;
  116. }
  117.  
  118. void    sort( int sorttype, int argtype, void *data, int n, int *retlank )
  119. {
  120.     int        i;
  121.  
  122.     type = argtype;
  123.     base = data;
  124.     element = n;
  125.     lank = retlank;
  126.  
  127.     for( i=0; i<element; i++ )
  128.         *(lank+i) = i;
  129.  
  130.     switch( sorttype )
  131.     {
  132.         case SORT_SHELL:
  133.             shellsort();
  134.             break;
  135.         case SORT_BUBBLE:
  136.             bubblesort();
  137.             break;
  138.     }
  139.  
  140.     return;
  141. }
  142.