Sorting Array of Strings

Sort by

recency

|

238 Discussions

|

  • + 0 comments

    Here is my take on this.

    int lexicographic_sort(const char* a, const char* b) {
        while (*(a) && *(b)) 
        {
            if(*a > *b) return 1;
            if(*a < *b) return -1;
            a++;
            b++;
        };
        if (*a) return 1;
        if (*b) return -1;
        
        return 0;
    }
    
    int lexicographic_sort_reverse(const char* a, const char* b) {
        while (*(a) && *(b)) 
        {
            if(*a > *b) return -1;
            if(*a < *b) return 1;
            a++;
            b++;
        };
        if (*a) return -1;
        if (*b) return 1;
        
        return 0;
    }
    
    int sort_by_number_of_distinct_characters(const char* a, const char* b) {
        int lena = strlen(a), lenb = strlen(b);
        char *arra = NULL, *arrb = NULL;
        char *arra_ad = NULL, *arrb_ad = NULL;
        int arra_sz = 0, arrb_sz = 0;
        const char *a_ad = a, *b_ad = b;
    
    
        arra = (char *)calloc(lena+1, sizeof(char));
        arrb = (char *)calloc(lenb+1, sizeof(char));
        arra_ad = arra;
        arrb_ad = arrb;
    
        while(*a || *b)
        {
            if (*a)
            {
                arra = arra_ad;
    
                while (*arra)
                {
                    if(*a == *arra) goto founda;
                    arra++;
                }
                arra_ad[arra_sz] = *a;
                arra_sz++;
    
                founda:;
                a++;
            }
            if (*b)
            {
                arrb = arrb_ad;
    
                while (*arrb)
                {
                    if(*b == *arrb) goto foundb;
                    arrb++;
                }
                arrb_ad[arrb_sz] = *b;
                arrb_sz++;
    
                foundb:;
                b++;
            }
        }
    
        free(arra_ad);
        free(arrb_ad);
    
        if(arra_sz > arrb_sz) return 1;
        if(arra_sz < arrb_sz) return -1;
    
        return lexicographic_sort(a_ad, b_ad);
    }
    
    int sort_by_length(const char* a, const char* b) {
        int lena = strlen(a), lenb = strlen(b);
        if (lena > lenb) return 1;
        if (lena < lenb) return -1;
    
        return lexicographic_sort(a, b);
    }
    
    void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)){
        char* temp = NULL;
        int i = 0, j = 0;
    
        for(i = len - 1; i > 0; i--)
        {
            for(j = 0; j < i; j++)
            {
                if (cmp_func(arr[j], arr[j+1])>0)
                {
                    temp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    
  • + 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[26];
        memset(char_count,1,sizeof(char_count));
    
        for (const char* p = a; *p != '\0'; p++) {
            if (char_count[*p - 'a']) {
                char_count[*p - 'a'] = 0;
                count_a++;
            }
        }
    
        memset(char_count, 1, sizeof(char_count));
    
        for (const char* p = b; *p != '\0'; p++) {
            if (char_count[*p - 'a']) {
                char_count[*p - 'a'] = 0;
                count_b++;
            }
        }
    
        if (count_a == count_b) {
            return strcmp(a, b);
        } else {
            return count_a - count_b;
        }
        return 0;
    }
    
    int sort_by_length(const char* a, const char* b) {
        if(strlen(a) > strlen(b)){
            return 1;
        }
        else if(strlen(a) < strlen(b)){
            return -1;
        }
        else if(strlen(a) == strlen(b)){
            return strcmp(a, b);
        }
        return 0;
    }
    
    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++) {
            if (cmp_func(arr[i], arr[i+1]) > 0) {
                char* temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
                i = -1;
            }
        }
    }
    
  • + 0 comments

    Here is Sorting Arrays of Strings solution in c - https://programmingoneonone.com/hackerrank-sorting-array-of-strings-solution-in-c.html

  • + 0 comments

    My submission

    `

    include

    include

    include

    int lexicographic_sort(const char* a, const char* b) { if (a == NULL && b == NULL) return 0; // Both are NULL

    if (a == NULL) return 1; 
    
    if (b == NULL) return -1; 
    
    
    // Handle empty strings
    if (*a == '\0' && *b == '\0') return 0; 
    
    if (*a == '\0') return 1;
    
    if (*b == '\0') return -1; 
    
     while (*a != '\0' && *b != '\0') {
    
        if (*b < *a) {
    
            return -1; // a is less than b
    
        } else if (*b > *a) {
    
            return 1; // a is greater than b
    
        } 
        a++;
    
        b++;
    
    }
    
    // If we reach here, one of the strings is a prefix of the other
    
    return (*b == '\0' && *a != '\0') ? -1 : (*b != '\0' && *a == '\0') ? 1 : 0;
    

    }

    int lexicographic_sort_reverse(const char* a, const char* b) {

    int r = lexicographic_sort(a,b);
    return r == -1? 1: r== 1 ? -1:0;
    }
    

    int count_distinct_characters(const char* str) {

    int char_count[256] = {0}; // Assuming ASCII characters
    
    int distinct_count = 0;
    
    
    while (*str) {
    
        if (char_count[(unsigned char)*str] == 0) {
    
            distinct_count++;
    
        }
    
        char_count[(unsigned char)*str]++;
    
        str++;
    
    }
    
    
    return distinct_count;
    

    }

    int sort_by_number_of_distinct_characters(const char* a, const char* b) { if (a == NULL && b == NULL) return 0; // Both are NULL

    if (a == NULL) return 1; 
    
    if (b == NULL) return -1; 
    
    
    // Handle empty strings
    
    if (*a == '\0' && *b == '\0') return 0; 
    
    if (*a == '\0') return 1;
    
    if (*b == '\0') return -1; 
    
    int distinct_a = count_distinct_characters(a);
    
    int distinct_b = count_distinct_characters(b);
    
    
    // Sort in ascending order of distinct character counts
    
    return  distinct_b-distinct_a == 0 ? lexicographic_sort(a,b):distinct_b-distinct_a; // Returns negative if a < b, positive if a > b
    

    }

    int sort_by_length(const char* a, const char* b) { // Store original pointers const char* orig_a = a; const char* orig_b = b;

    int len_a =0; int len_b = 0; for(; *a != 0; len_a++, *a++ ); for(; *b != 0; len_b++, *b++ );

    if (len_a == len_b) return lexicographic_sort(orig_a, orig_b);

    return len_b -len_a ;

    }

    void string_sort(char** arr,const int len,int (cmp_func)(const char a, const char* b)){

    for (int i = 0; i < len ; i++) {

        int min_index = i; // Start with the current index as the minimum
    
    
        for (int j = i + 1; j < len; j++) {
    
            if (cmp_func( *(arr +min_index), *(arr +j)) <= -1) {
             min_index = j; // Update min_index if a smaller element is found
    
            }
    
        }
    
    
        // Swap the found minimum element with the first element
    
        if (min_index != i) {
    
            char* temp = *(arr +i);
    
          *(arr +i) = *(arr +min_index);
    
           *(arr +min_index) = temp;
    
        }
    
    }
    

    }

    `

  • + 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 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); 
    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)){ // Bubble Sort for(int i = 0; i < len - 1; i++){ for(int j = 0; j < len - i - 1; j++){ if (cmp_func(arr[j], arr[j + 1]) > 0) { char* temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }