You are viewing a single comment's thread. Return to all comments →
it is giving wrong answer in the length char sorting. what wrong? int lexicographic_sort(const char* a, const char* b) { return strcmp(a,b); }
int lexicographic_sort_reverse(const char* a, const char* b) { return (-1)*lexicographic_sort(a,b); }
int sort_by_number_of_distinct_characters(const char* a, const char* b) { int la=strlen(a),ca=0; int lb=strlen(b),cb=0,flag=0; for(int i=0;i
}
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 = 0; i < len; i++) { int min_idx = i; for (int j = i+1; j < len; j++) { if (cmp_func(arr[i], arr[j]) > 0) { char* temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting Array of Strings
You are viewing a single comment's thread. Return to all comments →
it is giving wrong answer in the length char sorting. what wrong? int lexicographic_sort(const char* a, const char* b) { return strcmp(a,b); }
int lexicographic_sort_reverse(const char* a, const char* b) { return (-1)*lexicographic_sort(a,b); }
int sort_by_number_of_distinct_characters(const char* a, const char* b) { int la=strlen(a),ca=0; int lb=strlen(b),cb=0,flag=0; for(int i=0;i
}
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 = 0; i < len; i++) { int min_idx = i; for (int j = i+1; j < len; j++) { if (cmp_func(arr[i], arr[j]) > 0) { char* temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } }
}