Sorting Array of Strings

Sort by

recency

|

232 Discussions

|

  • + 0 comments

    include

    include

    include

    int lexicographic_sort(const char* a, const char* b) { return strcmp(a, b); }

    int lexicographic_sort_reverse(const char* a, const char* b) { return strcmp(b, a); }

    int sort_by_number_of_distinct_characters(const char* a, const char* b) { int count_a = 0, count_b = 0; int freq_a[26] = {0}, freq_b[26] = {0};

    for (int i = 0; i < strlen(a); i++) {
        if (freq_a[a[i] - 'a'] == 0) {
            count_a++;
            freq_a[a[i] - 'a'] = 1;
        }
    }
    
    for (int i = 0; i < strlen(b); i++) {
        if (freq_b[b[i] - 'a'] == 0) {
            count_b++;
            freq_b[b[i] - 'a'] = 1;
        }
    }
    
    if (count_a == count_b) {
        return strcmp(a, b);
    } else {
        return (count_a - count_b);
    }
    

    }

    int sort_by_length(const char* a, const char* b) { int len_a = strlen(a); int len_b = strlen(b);

    if (len_a == len_b) {
        return strcmp(a, b);
    } else {
        return (len_a - len_b);
    }
    

    }

    void string_sort(char** arr, const int len, int (cmp_func)(const char a, const char* b)) { for (int i = 0; i < len; i++) { for (int j = i + 1; j < len; j++) { if (cmp_func(arr[i], arr[j]) > 0) { char* temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }

    int main() { int n; scanf("%d", &n);

    char** arr;
    arr = (char**)malloc(n * sizeof(char*));
    
    for(int i = 0; i < n; i++){
        *(arr + i) = malloc(1024 * sizeof(char));
        scanf("%s", *(arr + i));
        *(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);
    }
    
    string_sort(arr, n, lexicographic_sort);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]);
    printf("\n");
    
    string_sort(arr, n, lexicographic_sort_reverse);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]); 
    printf("\n");
    
    string_sort(arr, n, sort_by_length);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]);    
    printf("\n");
    
    string_sort(arr, n, sort_by_number_of_distinct_characters);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]); 
    printf("\n");
    

    }

  • + 0 comments

    include

    include

    include

    int lexicographic_sort(const char* a, const char* b) { //increasing order return strcmp(a, b); }

    int lexicographic_sort_reverse(const char* a, const char* b) { //decreasing order return strcmp(b, a); }

    int sort_by_number_of_distinct_characters(const char* a, const char* b){ //increasing order int lena = strlen(a); int lenb = strlen(b); int counta = 0; int checka = 0; int countb = 0; int checkb = 0; for(int i=0;i

    void string_sort(char** arr,const int len,int (cmp_func)(const char a, const char* b)){ for(int i=0;i0){ char *temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }

    int main() { int n; scanf("%d", &n);

    char** arr;
    arr = (char**)malloc(n * sizeof(char*));
    
    for(int i = 0; i < n; i++){
        *(arr + i) = malloc(1024 * sizeof(char));
        scanf("%s", *(arr + i));
        *(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);
    }
    
    string_sort(arr, n, lexicographic_sort);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]);
    printf("\n");
    
    string_sort(arr, n, lexicographic_sort_reverse);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]); 
    printf("\n");
    
    string_sort(arr, n, sort_by_length);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]);    
    printf("\n");
    
    string_sort(arr, n, sort_by_number_of_distinct_characters);
    for(int i = 0; i < n; i++)
        printf("%s\n", arr[i]); 
    printf("\n");
    

    }

  • + 0 comments
    int lexicographic_sort(const char* a, const char* b) {
        return strcmp(a, b);
    
    }
    
    int lexicographic_sort_reverse(const char* a, const char* b) {
        return strcmp(b, a);
    
    }
    
    int sort_by_number_of_distinct_characters(const char* a, const char* b) {
        int count_a = 0, count_b = 0;
        int char_count[30] = {0};
    
        for (const char* p = a; *p; p++) {
            if (!char_count[*p - 'a']) {
                char_count[*p - 'a'] = 1;
                count_a++;
            }
        }
    
        memset(char_count, 0, sizeof(char_count));
    
        for (const char* p = b; *p != '\0'; p++) {
            if (!char_count[*p - 'a']) {
                char_count[*p - 'a'] = 1;
                count_b++;
            }
        }
    
        if (count_a == count_b) {
            return strcmp(a, b);
        } else {
            return count_a - count_b;
        }
        
    }
    
    int sort_by_length(const char* a, const char* b) {
        int len_a = strlen(a);
        int len_b = strlen(b);
    
        if (len_a == len_b) {
            return strcmp(a, b);
        } else {
            return len_a - len_b;
        }
        
    }
    
    
    void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){
                for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                if (cmp_func(arr[i], arr[j]) > 0) {
                    char* temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int lexicographic_sort(const char* a, const char* b) {
        return strcmp(a, b);
    
    }
    
    int lexicographic_sort_reverse(const char* a, const char* b) {
        return strcmp(b, a);
    
    }
    
    int sort_by_number_of_distinct_characters(const char* a, const char* b) {
        int count_a = 0, count_b = 0;
        int char_count[26] = {0};
    
        for (const char* p = a; *p; p++) {
            if (!char_count[*p - 'a']) {
                char_count[*p - 'a'] = 1;
                count_a++;
            }
        }
    
        memset(char_count, 0, sizeof(char_count));
    
        for (const char* p = b; *p; p++) {
            if (!char_count[*p - 'a']) {
                char_count[*p - 'a'] = 1;
                count_b++;
            }
        }
    
        if (count_a == count_b) {
            return strcmp(a, b);
        } else {
            return count_a - count_b;
        }
        
    }
    
    int sort_by_length(const char* a, const char* b) {
        int len_a = strlen(a);
        int len_b = strlen(b);
    
        if (len_a == len_b) {
            return strcmp(a, b);
        } else {
            return len_a - len_b;
        }
        
    }
    
    void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){
                for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                if (cmp_func(arr[i], arr[j]) > 0) {
                    char* temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    
    
    int main() 
    {
        int n;
        scanf("%d", &n);
      
        char** arr;
    	arr = (char**)malloc(n * sizeof(char*));
      
        for(int i = 0; i < n; i++){
            *(arr + i) = malloc(1024 * sizeof(char));
            scanf("%s", *(arr + i));
            *(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);
        }
      
        string_sort(arr, n, lexicographic_sort);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]);
        printf("\n");
    
        string_sort(arr, n, lexicographic_sort_reverse);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]); 
        printf("\n");
    
        string_sort(arr, n, sort_by_length);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]);    
        printf("\n");
    
        string_sort(arr, n, sort_by_number_of_distinct_characters);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]); 
        printf("\n");
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int lexicographic_sort(const char* a, const char* b) {
        return strcmp(a, b);
    }
    
    int lexicographic_sort_reverse(const char* a, const char* b) {
        return strcmp(b, a);
    }
    
    int sort_by_number_of_distinct_characters(const char* a, const char* b) {
        int countA[26] = {0};
        int distinctA = 0;
        for(int i = 0; a[i] != '\0'; i++){
            if(countA[a[i] - 'a'] == 0){
                distinctA++;
                countA[a[i] - 'a'] = 1;                        
            }
        }
        int countB[26] = {0};
        int distinctB = 0;
        for(int i = 0; b[i] != '\0'; i++){
            if(countB[b[i] - 'a'] == 0){
                distinctB++;
                countB[b[i] - 'a'] = 1;                        
            }
        }
        if(distinctA != distinctB){
            return (distinctA - distinctB);
        }
        else{
            return strcmp(a, b);        
        }        
    }
    
    int sort_by_length(const char* a, const char* b) {
        int lengthA = strlen(a);
        int lengthB = strlen(b);
        if(lengthA != lengthB){
            return (lengthA - lengthB);
        }
        else{
            return strcmp(a, b);        
        }    
    }
    
    void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){
        for(int i = 0; i < len - 1; i++){
            for(int j = 0; j < len - 1 - i; j++){
                if(cmp_func(arr[j], arr[j + 1]) > 0){
                    char* temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    
    
    int main() 
    {
        int n;
        scanf("%d", &n);
      
        char** arr;
    	arr = (char**)malloc(n * sizeof(char*));
      
        for(int i = 0; i < n; i++){
            *(arr + i) = malloc(1024 * sizeof(char));
            scanf("%s", *(arr + i));
            *(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1);
        }
      
        string_sort(arr, n, lexicographic_sort);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]);
        printf("\n");
    
        string_sort(arr, n, lexicographic_sort_reverse);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]); 
        printf("\n");
    
        string_sort(arr, n, sort_by_length);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]);    
        printf("\n");
    
        string_sort(arr, n, sort_by_number_of_distinct_characters);
        for(int i = 0; i < n; i++)
            printf("%s\n", arr[i]); 
        printf("\n");
    }