00001
00008 #include "dkcSort.h"
00009
00010
00011
00012
00013
00014
00015
00016 void WINAPI dkcShellSort( void *base,size_t num,size_t width,DKC_SORT_COMPARE_TYPE compare){
00017
00018
00019 int h, i, j;
00020 int n = (int)num;
00021 BYTE *x = malloc(width);
00022 BYTE *a = (BYTE *)base;
00023
00024
00025
00026 h = 13;
00027 while (h < n){
00028 h = 3 * h + 1;
00029 }
00030
00031 h = ( h - 1 ) / 3;
00032 while (h > 0) {
00033 for (i = h; i < n; i++) {
00034
00035
00036 dkcSwap(x,&a[i * width],width);
00037 for (j = i - h;
00038 j >= 0 ;
00039 j -= h)
00040 {
00041 if(compare(&a[j * width],x) > 0)
00042 {
00043
00044 dkcSwap(&a[(j + h) * width] , &a[j * width],width);
00045 }else{
00046 break;
00047 }
00048 }
00049
00050
00051 dkcSwap(&a[(j + h) * width],x ,width);
00052 }
00053
00054 h = ( h - 1 ) / 3;
00055 }
00056 free(x);
00057 }
00058
00059 void WINAPI dkcBubbleSort( void *base,size_t num,size_t width,DKC_SORT_COMPARE_TYPE compare)
00060 {
00061 BYTE *b = (BYTE *)base;
00062 size_t i, j;
00063
00064 for(i=0; i<num-1; i++){
00065 for(j=num-1; j>i; j--){
00066 if(compare(&b[(j-1) * width],&b[j*width]) > 0)
00067 {
00068 dkcSwap(&b[(j-1) * width],&b[j*width],width);
00069 }
00070 }
00071 }
00072
00073
00074
00075 }
00076
00077
00078
00079
00080
00081
00082
00083 void WINAPI dkcCombSort( void *base,size_t num,size_t width,DKC_SORT_COMPARE_TYPE compare){
00084
00085 BYTE *b = (BYTE *)base;
00086 int gap = num;
00087 size_t first2 = num;
00088 BOOL swapped = FALSE;
00089 int newgap;
00090 size_t i,j;
00091 if ( gap < 1 ) {
00092 dkcBubbleSort(base,num,width,compare);
00093 return;
00094 }
00095
00096 do {
00097 newgap = (gap*10+3)/13;
00098 if ( newgap < 1 ) newgap = 1;
00099 first2 += newgap - gap;
00100 gap = newgap;
00101 swapped = FALSE;
00102 for (i = 0, j = first2;
00103 j != num;
00104 ++i, ++j) {
00105
00106 if(compare(&b[i*width],&b[j*width]) > 0)
00107 {
00108 dkcSwap(&b[j*width],&b[i*width],width);
00109 swapped = TRUE;
00110 }
00111 }
00112 } while ( (gap > 1) || swapped );
00113 }
00114
00115
00116
00117 void WINAPI dkcQuickSort( void *base,size_t num,size_t width,DKC_SORT_COMPARE_TYPE compare)
00118 {
00119 qsort(base,num,width,compare);
00120 }
00121
00122
00123 #if 0
00124 void WINAPI dkcBlockSortEncode(void *buff,size_t buffsize){
00125
00126
00127
00128 }
00129
00130 void WINAPI dkcBlockSortDecode(void *buff,size_t buffsize){
00131
00132
00133
00134
00135
00136 }
00137 #endif
00138
00139
00140 int WINAPI dkcDistCountSortInt(size_t num, const int *a, int *b,int Min_,int Max_)
00141 {
00142
00143 int i, x;
00144 size_t ii;
00145
00146
00147
00148 int *count = (int *)dkcAllocate(sizeof(int) * (Max_ + Min_ + 1 ));
00149 if(NULL==count){
00150 return edk_OutOfMemory;
00151 }
00152
00153
00154
00155
00156 for (ii = 0; ii < num; ii++){
00157 count[a[ii] - Min_]++;
00158 }
00159
00160 for (i = 1; i <= Max_ - Min_; i++){
00161 count[i] += count[i - 1];
00162 }
00163 for (i = num - 1; i >= 0; i--) {
00164 x = a[i] - Min_; b[--count[x]] = a[i];
00165
00166 }
00167 dkcFree((void **)&count);
00168 return edk_SUCCEEDED;
00169
00170 }
00171
00172 int WINAPI dkcDistCountSortShort(size_t num, const short *a, short *b,short Min_,short Max_)
00173 {
00174 int i, x;
00175 size_t ii;
00176
00177 short *count = (short *)dkcAllocate(sizeof(short) * (Max_));
00178 if(NULL==count){
00179 return edk_OutOfMemory;
00180 }
00181 for (ii = 0; ii < num; ii++){
00182 count[a[ii] - Min_]++;
00183 }
00184
00185 for (i = 1; i <= Max_ - Min_; i++){
00186
00187 count[i] = (short)(count[i] + count[i - 1]);
00188 }
00189 for (i = num - 1; i >= 0; i--) {
00190 x = a[i] - Min_; b[--count[x]] = a[i];
00191 }
00192 dkcFree((void **)&count);
00193 return edk_SUCCEEDED;
00194 }
00195
00196 int WINAPI dkcDistCountSortChar(size_t num, const char *a, char *b,char Min_,char Max_)
00197 {
00198 int i, x;
00199 size_t ii;
00200
00201 char *count = (char *)dkcAllocate(sizeof(char) * (Max_));
00202 if(NULL==count){
00203 return edk_OutOfMemory;
00204 }
00205 for (ii = 0; ii < num; ii++){
00206 count[a[ii] - Min_]++;
00207 }
00208
00209 for (i = 1; i <= Max_ - Min_; i++){
00210
00211 count[i] = (char)(count[i] + count[i - 1]);
00212 }
00213 for (i = num - 1; i >= 0; i--) {
00214 x = a[i] - Min_; b[--count[x]] = a[i];
00215 }
00216 dkcFree((void **)&count);
00217 return edk_SUCCEEDED;
00218
00219
00220 }