C- void q_sort(char* arr[], const int len) {
-
- typedef struct _Range {
- int start, end;
- } Range;
-
- Range new_Range(int s, int e) {
- Range r;
- r.start = s;
- r.end = e;
- return r;
- }
-
- if (len <= 0)
- return;
-
- Range r[len];
- int p = 0;
- r[p++] = new_Range(0, len - 1);
- while (p) {
- Range range = r[--p];
- if (range.start >= range.end)
- continue;
- char* mid = arr[range.end], * cp;
- int left = range.start, right = range.end - 1;
- while (left < right) {
- while (GTR(mid, arr[left]) && left < right)
- left++;
- while (!GTR(mid, arr[right]) && left < right)
- right--;
- cp = arr[left], arr[left] = arr[right], arr[right] = cp;
- }
- if (!GTR(mid, arr[left]))
- cp = arr[left], arr[left] = arr[range.end], arr[range.end] = cp;
- else
- left++;
- r[p++] = new_Range(range.start, left - 1);
- r[p++] = new_Range(left + 1, range.end);
- }
- }
-
- int GTR(char* s1, char* s2) {
-
- char * p1 = s1, * p2 = s2;
-
- while (1) {
-
- if (!*p1 && !*p2) return 1;
-
- if (!*p1) p1 = s2;
-
- if (!*p2) p2 = s1;
-
- if (*p1 > *p2)
- return 1;
- else if (*p1 < *p2)
- return 0;
- else p1++, p2++;
-
- }
- }
-
- char* largestNumber(int* nums, int numsSize) {
-
- char strs [numsSize][11], * strPtrs[numsSize], * cp1, * cp2, * strRet = malloc(10 * numsSize + 1), ch;
-
- int i, j, k, t;
-
- for (i = 0; i < numsSize; i++) {
-
- t = *(nums + i);
- j = 0;
- k = -1;
-
- // digits convert to string
- do {
- strs[i][j++] = '0' + t % 10;
- } while ((t /= 10) > 0);
- strs[i][j] = '\0';
-
- // reverse string
- *(strPtrs + i) = cp1 = *(strs + i);
- while (--j > ++k)
- * (cp1 + j) ^= *(cp1 + k), * (cp1 + k) ^= *(cp1 + j), * (cp1 + j) ^= *(cp1 + k);
-
- }
-
- q_sort(strPtrs, numsSize);
-
- // printf("After q_sort\n");
- // for (i = numsSize - 1; i >= 0; i--) printf("%s\n", strPtrs[i]);
-
- cp2 = strRet;
-
- if (strPtrs[numsSize - 1][0] == '0') {
- *strRet = '0';
- *(strRet + 1) = '\0';
- return strRet;
- }
-
- for (i = numsSize - 1; i >= 0; i--) {
-
- cp1 = *(strPtrs + i);
- while (ch = *(cp1++)) *(cp2++) = ch;
-
- }
- *cp2 = '\0';
-
- return strRet;
- }
复制代码
|