Sorting Array of Strings in C - Online Judge

Latest

This is an Online Judge Solution Base Site. We can discuss & Solve any contest solution in Programming.

Monday, March 23, 2020

Sorting Array of Strings in C

Sample Input 0
4
wkue
qoi
sbv
fekls
Sample Output 0
fekls
qoi
sbv
wkue
wkue
sbv
qoi
fekls
qoi
sbv
wkue
fekls
qoi
sbv
wkue
fekls

Solution in C:-


#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 distinct_chars(const char *a)
{
int dist = 0;
while (*a != '\0') {
if (!strchr(a + 1, *a))
dist++;
a++;
}
return dist;
}



int sort_by_number_of_distinct_characters(const char* a, const char* b) {
int res = distinct_chars(a) - distinct_chars(b);
return (res) ? res : lexicographic_sort(a, b);
}

int sort_by_length(const char* a, const char* b) {
int res = strlen(a) - strlen(b);
return (res) ? res : lexicographic_sort(a, b);
}

/* simple bubble sort :) */

void string_sort(char** arr, const int len,int (*cmp_func)(const char* a, const char* b)) {
int sorted = 0;
while (!sorted) {
sorted = 1;
for (int i = 0; i < len - 1; i++) {

if (cmp_func(arr[i], arr[i + 1]) > 0) {
char *tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
sorted = 0;
}
}
}
}

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");
}

No comments:

Post a Comment

Thanks..