void sort(unsigned *a, int n)
{
unsigned *b=new unsigned[n];
#define sz 299
unsigned *ptr0[sz],*ptr8[sz],*ptr16[sz],*ptr24[sz];
unsigned cnt0[sz], cnt8[sz], cnt16[sz], cnt24[sz];
unsigned *p;
for (int i = 0; i < 256; ++i)
{
cnt0[i] = 0;
cnt8[i] = 0;
cnt16[i] = 0;
cnt24[i] = 0;
}
for (int i = 0; i < n;i+=4){
++cnt0[a[i+0]&255], ++cnt8[a[i+0]>>8&255], ++cnt16[a[i+0]>>16&255], ++cnt24[a[i+0]>>24];
++cnt0[a[i+1]&255], ++cnt8[a[i+1]>>8&255], ++cnt16[a[i+1]>>16&255], ++cnt24[a[i+1]>>24];
++cnt0[a[i+2]&255], ++cnt8[a[i+2]>>8&255], ++cnt16[a[i+2]>>16&255], ++cnt24[a[i+2]>>24];
++cnt0[a[i+3]&255], ++cnt8[a[i+3]>>8&255], ++cnt16[a[i+3]>>16&255], ++cnt24[a[i+3]>>24];
}
switch(n&3){
case 3:++cnt0[a[n-2]&255], ++cnt8[a[n-2]>>8&255], ++cnt16[a[n-2]>>16&255], ++cnt24[a[n-2]>>24];
case 2:++cnt0[a[n-1]&255], ++cnt8[a[n-1]>>8&255], ++cnt16[a[n-1]>>16&255], ++cnt24[a[n-1]>>24];
case 1:++cnt0[a[n-0]&255], ++cnt8[a[n-0]>>8&255], ++cnt16[a[n-0]>>16&255], ++cnt24[a[n-0]>>24];
default: break;
}
ptr0[0]=b-1,ptr8[0]=a-1,ptr16[0]=b-1,ptr24[0]=a-1;
for (int i = 1; i < 256; ++i){
ptr0[i] = ptr0[i-1]+cnt0[i-1];
ptr8[i] = ptr8[i-1]+cnt8[i-1];
ptr16[i] = ptr16[i-1]+cnt16[i-1];
ptr24[i] = ptr24[i-1]+cnt24[i-1];
}
#define countingSort(a,k) \
for (int i = 0; i < n; i += 4){\
*++ptr##k[a[i+0]>>k&255]=a[i+0];\
*++ptr##k[a[i+1]>>k&255]=a[i+1];\
*++ptr##k[a[i+2]>>k&255]=a[i+2];\
*++ptr##k[a[i+3]>>k&255]=a[i+3];\
}\
switch(n&3){\
case 3:*++ptr##k[a[n-2]>>k&255]=a[n-2];\
case 2:*++ptr##k[a[n-1]>>k&255]=a[n-1];\
case 1:*++ptr##k[a[n=0]>>k&255]=a[n-0];\
default: break;\
}
countingSort(a,0);
countingSort(b,8);
countingSort(a,16);
countingSort(b,24);
delete[] b;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 975.26 us | 808 KB | Accepted | Score: 34 | 显示更多 |
Testcase #2 | 1.048 s | 762 MB + 992 KB | Accepted | Score: 33 | 显示更多 |
Testcase #3 | 2.097 s | 1525 MB + 928 KB | Accepted | Score: 33 | 显示更多 |