Skip to content

Commit 4a0da0b

Browse files
committed
Add implementation for Big Sorting in C
Implemented the Big Sorting solution with a custom comparator for sorting large numerical strings. This includes input handling, sorting logic, and memory management. The program reads input, sorts strings based on length and lexicographic order, and outputs the results.
1 parent b25288b commit 4a0da0b

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

BigSorting.c

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
#include <assert.h>
2+
#include <ctype.h>
3+
#include <limits.h>
4+
#include <math.h>
5+
#include <stdbool.h>
6+
#include <stddef.h>
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
char* readline();
13+
char* ltrim(char*);
14+
char* rtrim(char*);
15+
16+
int parse_int(char*);
17+
18+
/*
19+
* Complete the 'bigSorting' function below.
20+
*
21+
* The function is expected to return a STRING_ARRAY.
22+
* The function accepts STRING_ARRAY unsorted as parameter.
23+
*/
24+
25+
/*
26+
* To return the string array from the function, you should:
27+
* - Store the size of the array to be returned in the result_count variable
28+
* - Allocate the array statically or dynamically
29+
*
30+
* For example,
31+
* char** return_string_array_using_static_allocation(int* result_count) {
32+
* *result_count = 5;
33+
*
34+
* static char* a[5] = {"static", "allocation", "of", "string", "array"};
35+
*
36+
* return a;
37+
* }
38+
*
39+
* char** return_string_array_using_dynamic_allocation(int* result_count) {
40+
* *result_count = 5;
41+
*
42+
* char** a = malloc(5 * sizeof(char*));
43+
*
44+
* for (int i = 0; i < 5; i++) {
45+
* *(a + i) = malloc(20 * sizeof(char));
46+
* }
47+
*
48+
* *(a + 0) = "dynamic";
49+
* *(a + 1) = "allocation";
50+
* *(a + 2) = "of";
51+
* *(a + 3) = "string";
52+
* *(a + 4) = "array";
53+
*
54+
* return a;
55+
* }
56+
*
57+
*/
58+
#include <stdio.h>
59+
#include <stdlib.h>
60+
#include <string.h>
61+
62+
int compare(const void* a, const void* b) {
63+
const char* str1 = *(const char**)a;
64+
const char* str2 = *(const char**)b;
65+
66+
// Compare based on length first
67+
size_t len1 = strlen(str1);
68+
size_t len2 = strlen(str2);
69+
70+
if (len1 < len2) {
71+
return -1;
72+
} else if (len1 > len2) {
73+
return 1;
74+
}
75+
76+
// If lengths are equal, compare lexicographically
77+
return strcmp(str1, str2);
78+
}
79+
80+
char** bigSorting(int unsorted_count, char** unsorted, int* result_count) {
81+
// Sort the array using qsort with the custom comparator
82+
qsort(unsorted, unsorted_count, sizeof(char*), compare);
83+
84+
// Set the result count to the number of elements
85+
*result_count = unsorted_count;
86+
87+
// Return the sorted array
88+
return unsorted;
89+
}
90+
91+
int main()
92+
{
93+
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
94+
95+
int n = parse_int(ltrim(rtrim(readline())));
96+
97+
char** unsorted = malloc(n * sizeof(char*));
98+
99+
for (int i = 0; i < n; i++) {
100+
char* unsorted_item = readline();
101+
102+
*(unsorted + i) = unsorted_item;
103+
}
104+
105+
int result_count;
106+
char** result = bigSorting(n, unsorted, &result_count);
107+
108+
for (int i = 0; i < result_count; i++) {
109+
fprintf(fptr, "%s", *(result + i));
110+
111+
if (i != result_count - 1) {
112+
fprintf(fptr, "\n");
113+
}
114+
}
115+
116+
fprintf(fptr, "\n");
117+
118+
fclose(fptr);
119+
120+
return 0;
121+
}
122+
123+
char* readline() {
124+
size_t alloc_length = 1024;
125+
size_t data_length = 0;
126+
127+
char* data = malloc(alloc_length);
128+
129+
while (true) {
130+
char* cursor = data + data_length;
131+
char* line = fgets(cursor, alloc_length - data_length, stdin);
132+
133+
if (!line) {
134+
break;
135+
}
136+
137+
data_length += strlen(cursor);
138+
139+
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
140+
break;
141+
}
142+
143+
alloc_length <<= 1;
144+
145+
data = realloc(data, alloc_length);
146+
147+
if (!data) {
148+
data = '\0';
149+
150+
break;
151+
}
152+
}
153+
154+
if (data[data_length - 1] == '\n') {
155+
data[data_length - 1] = '\0';
156+
157+
data = realloc(data, data_length);
158+
159+
if (!data) {
160+
data = '\0';
161+
}
162+
} else {
163+
data = realloc(data, data_length + 1);
164+
165+
if (!data) {
166+
data = '\0';
167+
} else {
168+
data[data_length] = '\0';
169+
}
170+
}
171+
172+
return data;
173+
}
174+
175+
char* ltrim(char* str) {
176+
if (!str) {
177+
return '\0';
178+
}
179+
180+
if (!*str) {
181+
return str;
182+
}
183+
184+
while (*str != '\0' && isspace(*str)) {
185+
str++;
186+
}
187+
188+
return str;
189+
}
190+
191+
char* rtrim(char* str) {
192+
if (!str) {
193+
return '\0';
194+
}
195+
196+
if (!*str) {
197+
return str;
198+
}
199+
200+
char* end = str + strlen(str) - 1;
201+
202+
while (end >= str && isspace(*end)) {
203+
end--;
204+
}
205+
206+
*(end + 1) = '\0';
207+
208+
return str;
209+
}
210+
211+
int parse_int(char* str) {
212+
char* endptr;
213+
int value = strtol(str, &endptr, 10);
214+
215+
if (endptr == str || *endptr != '\0') {
216+
exit(EXIT_FAILURE);
217+
}
218+
219+
return value;
220+
}

0 commit comments

Comments
 (0)