C++ (Qt)extern void numsort (int* arr, unsigned int size);extern int numunique (int* arr, unsigned int size);extern void numsort (unsigned int* arr, unsigned int size);extern int numunique (unsigned int* arr, unsigned int size);
C++ (Qt)unsigned char getbyte(int val, unsigned int digitpos) { if(digitpos == sizeof(int)) { return val>=0?1:0; } if(digitpos > 0) { val = val >> 8*digitpos; } val = val & 0xFF; return val;} void swap(unsigned int* val1, unsigned int* val2) { unsigned int val = *val1; *val1 = *val2; *val2 = val;} void digitsort(unsigned int* arr, unsigned int size, unsigned int digitpos) { unsigned int spos[256+1]; unsigned int npos[256]; memset(&spos, 0, (256+1)*sizeof(int)); for(int i=0; i<size; ++i) { spos[getbyte(arr[i], digitpos)+1]++; } for(int i=0; i<256; ++i) { spos[i+1] += spos[i]; } memcpy(&npos, &spos, 256*sizeof(int)); unsigned int val1; unsigned int val2; for(i=0; i<size;) { val1 = getbyte(arr[i], digitpos); if(i >= spos[val1] && i < npos[val1]) { i = npos[val1]; continue; } if(i == npos[val1]) { npos[val1]++; ++i; continue; } val2 = getbyte(arr[npos[val1]], digitpos); while(val2 == val1) { npos[val1]++; val2 = getbyte(arr[npos[val1]], digitpos); } swap(&arr[i], &arr[npos[val1]]); } if(!digitpos) {return;} digitpos--; for(int i=0; i<256; ++i) { size = spos[i+1]-spos[i]; if(!size) {continue;} digitsort(arr+spos[i], size, digitpos); }} void numsort(unsigned int* arr, unsigned int size) { if(!size) {return;} digitsort(arr, size, sizeof(int)-1);} void numsort(int* arr, unsigned int size) { if(!size) {return;} digitsort((unsigned int*)arr, size, sizeof(int));} int numunique (unsigned int* arr, unsigned int size) { if(!size) {return 0;} digitsort(arr, size, sizeof(int)-1); int n = 1; unsigned int val = arr[0]; for(int i=1; i<size; ++i) { if(val == arr[i]) {continue;} val = arr[i]; if(i != n) {arr[n] = val;} ++n; } return n;} int numunique (int* arr, unsigned int size) { if(!size) {return 0;} digitsort((unsigned int*)arr, size, sizeof(int)); int n = 1; int val = arr[0]; for(int i=1; i<size; ++i) { if(val == arr[i]) {continue;} val = arr[i]; if(i != n) {arr[n] = val;} ++n; } return n;}
if(digitpos == sizeof(int)) { return val>0?1:0; }
if(digitpos == sizeof(int)) { return val>=0?1:0; }
C++ (Qt)#include <iostream>#include <cstring>#include <algorithm>#include <ctime>#include <cstdlib>#include <cstdio>#include <limits> using namespace std; extern void numsort (int* arr, unsigned int size);extern int numunique (int* arr, unsigned int size);extern void numsort (unsigned int* arr, unsigned int size);extern int numunique (unsigned int* arr, unsigned int size); unsigned char getbyte(int val, unsigned int digitpos) { if(digitpos == sizeof(int)) { return val>=0?1:0; } if(digitpos > 0) { val = val >> 8*digitpos; } val = val & 0xFF; return val;} void swap(unsigned int* val1, unsigned int* val2) { unsigned int val = *val1; *val1 = *val2; *val2 = val;} void digitsort(unsigned int* arr, unsigned int size, unsigned int digitpos) { unsigned int spos[256+1]; unsigned int npos[256]; memset(&spos, 0, (256+1)*sizeof(int)); for(int i=0; i<size; ++i) { spos[getbyte(arr[i], digitpos)+1]++; } for(int i=0; i<256; ++i) { spos[i+1] += spos[i]; } memcpy(&npos, &spos, 256*sizeof(int)); unsigned int val1; unsigned int val2; for(int i=0; i<size;) { val1 = getbyte(arr[i], digitpos); if(i >= spos[val1] && i < npos[val1]) { i = npos[val1]; continue; } if(i == npos[val1]) { npos[val1]++; ++i; continue; } val2 = getbyte(arr[npos[val1]], digitpos); while(val2 == val1) { npos[val1]++; val2 = getbyte(arr[npos[val1]], digitpos); } swap(&arr[i], &arr[npos[val1]]); } if(!digitpos) {return;} digitpos--; for(int i=0; i<256; ++i) { size = spos[i+1]-spos[i]; if(!size) {continue;} digitsort(arr+spos[i], size, digitpos); }} void numsort(unsigned int* arr, unsigned int size) { if(!size) {return;} digitsort(arr, size, sizeof(int)-1);} void numsort(int* arr, unsigned int size) { if(!size) {return;} digitsort((unsigned int*)arr, size, sizeof(int));} int numunique (unsigned int* arr, unsigned int size) { if(!size) {return 0;} digitsort(arr, size, sizeof(int)-1); int n = 1; unsigned int val = arr[0]; for(int i=1; i<size; ++i) { if(val == arr[i]) {continue;} val = arr[i]; if(i != n) {arr[n] = val;} ++n; } return n;} int numunique (int* arr, unsigned int size) { if(!size) {return 0;} digitsort((unsigned int*)arr, size, sizeof(int)); int n = 1; int val = arr[0]; for(int i=1; i<size; ++i) { if(val == arr[i]) {continue;} val = arr[i]; if(i != n) {arr[n] = val;} ++n; } return n;} int main(){ srand(time(0)); const unsigned int size = 2000000; int arr[size]; for (int i = 0; i < size; ++i) { arr[i] = rand(); } clock_t tStart = clock(); //numsort(arr, size); sort(arr, arr+size); cout << (float)(clock() - tStart) / CLOCKS_PER_SEC << endl; return 0;}
C++ (Qt)max@T1000:~$ g++ --versiong++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
C++ (Qt)#include <iostream>#include <cstring>#include <algorithm>#include <ctime>#include <cstdlib>#include <cstdio>#include "gm_num_sort.h" using namespace std; int main(){ srand(time(0)); const unsigned int size = 2000000; int arr[size]; for (int i = 0; i < size; ++i) { arr[i] = rand(); } clock_t tStart = clock(); gm_NumSort(arr, size); cout << "gm_NumSort : " << (float)(clock() - tStart) / CLOCKS_PER_SEC << " sec" << endl; for (int i = 0; i < size; ++i) { arr[i] = rand(); } tStart = clock(); sort(arr, arr+size); cout << "std::sort : " << (float)(clock() - tStart) / CLOCKS_PER_SEC << " sec" << endl; return 0;}
C++ (Qt)gm_NumSort : 2.44 secstd::sort : 0.27 sec
C++ (Qt) srand(time(0)); const unsigned int size = 2000000; //2'000'000, 20'000'000, 200'000'000 int* arr = (int*) malloc(size*sizeof(int)); for (int i = 0; i < size; ++i) { arr[i] = rand(); } clock_t tStart = clock(); gm_NumSort(arr, size); cout << "gm_NumSort : " << (float)(clock() - tStart) / CLOCKS_PER_SEC << " sec" << endl; for (int i = 0; i < size; ++i) { arr[i] = rand(); } tStart = clock(); sort(arr, arr+size); cout << "std::sort : " << (float)(clock() - tStart) / CLOCKS_PER_SEC << " sec" << endl; return 0;
C++ (Qt) const unsigned int size = 2000000; int arr[size];
C++ (Qt)size = 2000000gm_NumSort : 2.45 secstd::sort : 0.27 sec
C++ (Qt)size = 20000000gm_NumSort : 11.16 secstd::sort : 3.3 sec
C++ (Qt)size = 200000000gm_NumSort : 31.8 secstd::sort : 38.51 sec
C++ (Qt)size = 2000000gm_NumSort : 0.99 secstd::sort : 0.27 sec
C++ (Qt)size = 20000000gm_NumSort : 5.6 secstd::sort : 3.31 sec
C++ (Qt)size = 200000000gm_NumSort : 25.21 secstd::sort : 38.76 sec