两次排序,但没有榜2大哥代码简短

#include <stdio.h>
void sort(int* arr, int beg, int end) {//ascending bubble sort
    for (int i = beg; i < end - 1; i++)
        for (int j = beg; j + 1 < end - (i - beg); j++)
            if (arr[j] > arr[j + 1]) {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
}
int main() {
    char str[1001];
    while (~scanf("%s", str)) {
        int len, ch[128], sum[128];
        for (len = 0; len < 128; len++) {
            ch[len] = len;
            sum[len] = 0;
        }
        //done:initialization
        for (len = 0; str[len]; len++)sum[str[len]]++;
        //done:statement
        for (int i = 0; i < 128 - 1; i++)
            for (int j = 0; j + 1 < 128 - i; j++)
                if (sum[j] < sum[j + 1]) {//descending bubble sort
                    int t = sum[j];
                    sum[j] = sum[j + 1];
                    sum[j + 1] = t;
                    t = ch[j];
                    ch[j] = ch[j + 1];
                    ch[j + 1] = t;
                }
        for (int i = 0; sum[i]; i++) {
            int j = i;
            while (sum[j] != sum[i])j++;
            sort(ch, i, j);
            i = j;
        }
        for (int i = 0; sum[i]; i++) {
            printf("%c", ch[i]);
        }
        printf("\n");
    }
}

附:榜2大哥代码,更简短,妙

#include <stdio.h>
#include <string.h>
 
int main()
{
    char str[1000] = {0};
    while (~scanf("%s", str)) {
        int cnt[200] = {0};
        int max = 0;
        for (int i = 0; i < strlen(str); i++) {
            cnt[str[i]]++;
            max = (max > cnt[str[i]]) ? max : cnt[str[i]];
        }
        for (int i = max; i > 0; i--) {
            //j为char
            for (char j = '0'; j < 'z'+1; j++) {
                if (cnt[j] == i) {
                    printf("%c", j);
                }
            }
        }
        printf("\n");
    }
    return 0;
}