Sorting Array of Strings

Sort by

recency

|

240 Discussions

|

  • + 0 comments
    int lexicographic_sort(const char* a, const char* b) {
        int i = 0;
        while( (a[i] != '\0') && (b[i] != '\0') )
        {
            if (a[i] > b[i])
                return 1;
            else if (a[i] < b[i])
                return 0;
            i++;
        }
        
        if (a[i] != '\0') return 1;
        return 0;
    }
    
    int lexicographic_sort_reverse(const char* a, const char* b) {
        int i = 0;
        while( (a[i] != '\0') && (b[i] != '\0') )
        {
            if (a[i] > b[i])
                return 0;
            else if (a[i] < b[i])
                return 1;
            i++;
        }
        
        if (b[i] != '\0') return 1;
        return 0;
    }
    
    int sort_by_number_of_distinct_characters(const char* a, const char* b) {
        char distA[26] = {0};
        char distB[26] = {0};
        
        int lenA = strlen(a);
        int lenB = strlen(b);
        
        for(int i = 0; i < lenA; i++)
        {
            if (a[i] >= 'a' && a[i] <= 'z')
            {
                distA[a[i] - 'a'] = 1;
            }
        }
        
        for(int i = 0; i < lenB; i++)
        {
            if (b[i] >= 'a' && b[i] <= 'z')
            {
                distB[b[i] - 'a'] = 1;
            }
        }
        
        int sumA = 0;
        int sumB = 0;
        for (int i = 0; i < 26; i++)
        {
            sumA += distA[i];
            sumB += distB[i];
        }
        
        if (sumA > sumB)
        {
            return 1;
        }
        else if (sumA == sumB) 
        {
            return lexicographic_sort(a, b);
        }
        else 
        {
            return 0;
        }
    }
    
    int sort_by_length(const char* a, const char* b) {
        int lenA = strlen(a);
        int lenB = strlen(b);
        
        if (lenA > lenB)
        {
            return 1;
        }
        else if (lenA == lenB)
        {
            return lexicographic_sort(a, b);
        }
        else 
        {
            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; i++)
        {
            for (int j = i + 1; j < len; j++)
            {
                if (cmp_func(arr[i], arr[j]))
                {
                    char *temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    
  • + 0 comments

    include

    include

    include

    // Comparison functions 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_length(const char* a, const char* b) { int len_a = strlen(a); int len_b = strlen(b); if (len_a != len_b) return len_a - len_b; return strcmp(a, b); }

    int sort_by_length_reverse(const char* a, const char* b) { int len_a = strlen(a); int len_b = strlen(b); if (len_a != len_b) return len_b - len_a; return strcmp(b, a); }

    // You might see this one in some variants: sort by number of distinct characters int count_distinct(const char* s) { int freq[26] = {0}; int count = 0; for (int i = 0; s[i]; i++) { if (!freq[s[i] - 'a']) { freq[s[i] - 'a'] = 1; count++; } } return count; }

    int sort_by_number_of_distinct_characters(const char* a, const char b) { int count_a = count_distinct(a); int count_b = count_distinct(b); if (count_a != count_b) return count_a - count_b; return strcmp(a, b); }

    // Generic sorting function using function pointer void string_sort(const char **arr, const int cnt, int (cmp_func)(const char, const char*)) { // Simple bubble sort - acceptable since constraints are small (n <= 100 usually) for (int i = 0; i < cnt; i++) { for (int j = i + 1; j < cnt; j++) { if (cmp_func(arr[i], arr[j]) > 0) { const char* temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }

    // The rest of the code (main, input handling) is usually provided by HackerRank

  • + 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