#include #include #include #include #define N 1000000 #define MIN_LEN 20 #define MAX_LEN 50 #define MAX_CHAR 'z' void tsort(char **s, int lo, int hi, int pos) { if (lo >= hi) { return; } int curr = lo, left = lo, right = hi; char mid = (s[lo][pos] + s[hi][pos]) / 2; char *tmp; while (curr <= right) { char c = s[curr][pos]; if (c < mid) { tmp = s[curr]; s[curr] = s[left]; s[left] = tmp; left++; curr++; } else if (c > mid) { tmp = s[curr]; s[curr] = s[right]; s[right] = tmp; right--; } else { curr++; } } tsort(s, lo, left - 1, pos); if (s[left][pos] != '\0' ) { tsort(s, left, right, pos + 1); } tsort(s, right + 1, hi, pos); } int main(void) { srand(time(NULL)); timeval tv; char* s[N]; // Generate some strings for (int i = 0; i < N; i++) { int len = rand() % (MAX_LEN - MIN_LEN + 1) + MIN_LEN; s[i] = (char*) malloc(len + 1); for (int j = 0; j < len; j++) { s[i][j] = rand() % (MAX_CHAR - 'a' + 1) + 'a'; } s[i][len] = '\0'; } gettimeofday (&tv, NULL); long long t1 = 1000LL * tv.tv_sec + tv.tv_usec / 1000; tsort(s, 0, N - 1, 0); gettimeofday (&tv, NULL); long long t2 = 1000LL * tv.tv_sec + tv.tv_usec / 1000; printf("Timp: %lld ms\n", t2 - t1); // Print them // for (int i = 0; i < N; i++) { // printf("[%s]\n", s[i]); // } }