- Prepare
- C
- Functions
- Sorting Array of Strings
- Discussions
Sorting Array of Strings
Sorting Array of Strings
+ 0 comments #include <stdio.h> #include <stdlib.h> #include <string.h> int lexicographic_sort(const char* a, const char* b) { return strcmp(a, b) > 0 ? 1 : 0; } int lexicographic_sort_reverse(const char* a, const char* b) { return strcmp(b, a) > 0 ? 1 : 0; } int get_distinct_chars(const char *s) { int chars[127] = {0}; int cnt = 0; while (*s != '\0') { if (chars[*s] == 0) { chars[*s]++; cnt++; } s++; } return cnt; } int sort_by_number_of_distinct_characters(const char* a, const char* b) { int distA = get_distinct_chars(a); int distB = get_distinct_chars(b); if (distA == distB) { return lexicographic_sort(a, b); } else if (distA > distB) { return 1; } return 0; } int sort_by_length(const char* a, const char* b) { int lenA = strlen(a); int lenB = strlen(b); if (lenA == lenB) { return lexicographic_sort(a, b); } else if (lenA > lenB) { return 1; } return 0; } void string_sort(char **arr, const int len, int (*cmp_func)(const char *a, const char *b)) { for (int i = 1; i < len; i++) { char *key = arr[i]; int j = i - 1; while (j >= 0 && cmp_func(arr[j], key) > 0) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } }
+ 0 comments int lexicographic_sort(const char* a, const char* b) { for (; *a && *b && *a == *b; ++a, ++b) ; return *a - *b; } int lexicographic_sort_reverse(const char* a, const char* b) { for (; *a && *b && *a == *b; ++a, ++b) ; return *b - *a; } int sort_by_length(const char* a, const char* b) { int n = strlen(a), m = strlen(b); if (n == m) return lexicographic_sort(a, b); return n - m; } int distinct_characters(const char* s) { char* d_str = (char *) malloc(1); d_str[0] = '\0'; for (int ctr = 0, i = 0; s[i]; ++i) { if (strchr(d_str, s[i]) == NULL) { d_str = realloc(d_str, strlen(d_str) + 2); d_str[ctr] = s[i]; d_str[++ctr] = '\0'; } } int d_chars = strlen(d_str); free(d_str); return d_chars; } int sort_by_number_of_distinct_characters(const char* a, const char* b) { int x = distinct_characters(a), y = distinct_characters(b); if (x == y) return lexicographic_sort(a, b); return x - y; } 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; } } } }
+ 1 comment The thing that bugged me for hours is that the 3rd output is for sort by length and the 4th output is for sort by distince characters. I kept getting the 3rd output wrong and I kept trying to find the non existent bug in the sort by length function. Really sucked the joy out of me.
+ 0 comments int lexicographic_sort(const char *a, const char *b) { return (strcmp(a, b) > 0) ? 1 : 0; } int lexicographic_sort_reverse(const char *a, const char *b) { return (strcmp(b, a) > 0) ? 1 : 0; } int distinct_characters(const char *str) { int characters[127] = {0}; int count = 0; while (*str != '\0') { if (characters[*str] == 0) { characters[*str]++; count++; } str++; } return count; } int sort_by_number_of_distinct_characters(const char *a, const char *b) { int distinctA = distinct_characters(a); int distinctB = distinct_characters(b); if (distinctA == distinctB) { return lexicographic_sort(a, b); } else if (distinctA > distinctB) { return 1; } return 0; } int sort_by_length(const char *a, const char *b) { int lenA = strlen(a); int lenB = strlen(b); if (lenA == lenB) { return lexicographic_sort(a, b); } else if (lenA > lenB) { return 1; } return 0; } void swap(char **s1, char **s2) { char *temp = *s1; *s1 = *s2; *s2 = temp; } 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))) { swap((arr + i), (arr + j)); } } } }
+ 0 comments include
include
include
int lexicographic_sort(const char* a, const char* b){ return strcmp(a, b) > 0; }
int lexicographic_sort_reverse(const char* a, const char* b){ return strcmp(a, b) <= 0; }
int sort_by_number_of_distinct_characters(const char* a, const char* b){ int c1 = 0, c2 = 0; int hsh1[26] = {0}, hsh2[26] = {0}; int n1 = strlen(a); int n2 = strlen(b);
for(int i = 0; i < n1; i++){ hsh1[a[i] - 'a'] = 1; } for(int i = 0; i < n2; i++){ hsh2[b[i] - 'a'] = 1; } for(int i = 0; i < 26; i++){ if(hsh1[i]) c1++; if(hsh2[i]) c2++; } if( c1 != c2) return c1 > c2; else return strcmp(a, b) > 0;
}
int sort_by_length(const char* a, const char* b){ if(strlen(a) != strlen(b)) return strlen(a) > strlen(b); else return strcmp(a, b) > 0; }
void string_sort(char** arr,const int len,int (cmp_func)(const char a, const char* b)){ for(int i = 1; i < len; i++){ int j = i; char* p = arr[i]; while(j > 0){ if((*cmp_func)(arr[j-1],p) > 0 ) arr[j] = arr[j-1]; else break; j--; } arr[j] = p; } }
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");
}
Sort 206 Discussions, By:
Please Login in order to post a comment