Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
cs_sort_template.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef STYPE
00012 # error "STYPE macro not defined"
00013 #endif
00014
00015 #ifndef SLT
00016 # error "SLT macro not defined"
00017 #endif
00018
00019 #ifndef SNAME
00020 # error "SNAME macro not defined"
00021 #endif
00022
00023 #ifndef TWO_TWO
00024 # define TWO_ONE(x,y) x ## y
00025 # define TWO_TWO(x,y) TWO_ONE(x,y)
00026 #endif
00027
00028 #define ISORT_NAME TWO_TWO(isort_,SNAME)
00029 #define QSREC_NAME TWO_TWO(qsrec_,SNAME)
00030 #define QSORT_NAME TWO_TWO(qsort_,SNAME)
00031
00032
00033
00034
00035 static void ISORT_NAME( int n , STYPE * ar )
00036 {
00037 register int j , p ;
00038 STYPE temp ;
00039 register STYPE *a = ar ;
00040
00041 if( n < 2 ) return ;
00042
00043 for( j=1 ; j < n ; j++ ){
00044
00045 if( SLT(a[j],a[j-1]) ){
00046 p = j ;
00047 temp = a[j] ;
00048
00049 do{
00050 a[p] = a[p-1] ;
00051 p-- ;
00052 } while( p > 0 && SLT(temp,a[p-1]) ) ;
00053
00054 a[p] = temp ;
00055 }
00056 }
00057 }
00058
00059
00060
00061
00062 #ifndef QS_STACK
00063 # define QS_STACK 1024
00064 #endif
00065
00066 #define QS_SWAPF(x,y) ( temp=(x),(x)=(y),(y)= temp)
00067 #define QS_SWAPI(i,j) (itemp=(i),(i)=(j),(j)=itemp)
00068
00069 static void QSREC_NAME( int n , STYPE * ar , int cutoff )
00070 {
00071 register int i , j ;
00072 STYPE temp , pivot ;
00073 register STYPE *a = ar ;
00074 int itemp ;
00075
00076 int left , right , mst , stack[QS_STACK] , nnew ;
00077
00078
00079
00080 if( cutoff < 3 ) cutoff = 3 ;
00081 if( n < cutoff ) return ;
00082
00083
00084
00085 stack[0] = 0 ;
00086 stack[1] = n-1 ;
00087 mst = 2 ;
00088
00089
00090
00091 while( mst > 0 ){
00092 right = stack[--mst] ;
00093 left = stack[--mst] ;
00094
00095 i = ( left + right ) / 2 ;
00096
00097
00098
00099 if( SLT(a[i],a[left]) ){ QS_SWAPF(a[left] ,a[i] ); }
00100 if( SLT(a[right],a[left]) ){ QS_SWAPF(a[left] ,a[right]); }
00101 if( SLT(a[right],a[left]) ){ QS_SWAPF(a[right],a[i] ); }
00102
00103 pivot = a[i] ;
00104 a[i] = a[right] ;
00105
00106 i = left ;
00107 j = right ;
00108
00109
00110
00111
00112 do{
00113 for( ; SLT(a[++i],pivot) ; ) ;
00114 for( ; SLT(pivot,a[--j]) ; ) ;
00115
00116 if( j <= i ) break ;
00117
00118 QS_SWAPF( a[i] , a[j] ) ;
00119 } while( 1 ) ;
00120
00121
00122
00123 a[right] = a[i] ;
00124 a[i] = pivot ;
00125
00126
00127
00128 nnew = 0 ;
00129 if( (i-left) > cutoff ){ stack[mst++] = left; stack[mst++] = i-1 ; nnew++; }
00130 if( (right-i) > cutoff ){ stack[mst++] = i+1 ; stack[mst++] = right; nnew++; }
00131
00132
00133
00134 if( nnew == 2 && stack[mst-3] - stack[mst-4] > stack[mst-1] - stack[mst-2] ){
00135 QS_SWAPI( stack[mst-4] , stack[mst-2] ) ;
00136 QS_SWAPI( stack[mst-3] , stack[mst-1] ) ;
00137 }
00138
00139 }
00140 }
00141
00142
00143
00144
00145 #ifndef QS_CUTOFF
00146 # define QS_CUTOFF 10
00147 #endif
00148
00149 void QSORT_NAME( int n , STYPE *a )
00150 {
00151 QSREC_NAME( n , a , QS_CUTOFF ) ;
00152 ISORT_NAME( n , a ) ;
00153 }
00154
00155 #undef ISORT_NAME
00156 #undef QSREC_NAME
00157 #undef QS_SWAPF
00158 #undef QS_SWAPI