We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the 'countingSort' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
/*
* To return the integer array from the function, you should:
* - Store the size of the array to be returned in the result_count variable
* - Allocate the array statically or dynamically
*
* For example,
* int* return_integer_array_using_static_allocation(int* result_count) {
* *result_count = 5;
*
* static int a[5] = {1, 2, 3, 4, 5};
*
* return a;
* }
*
* int* return_integer_array_using_dynamic_allocation(int* result_count) {
* *result_count = 5;
*
* int *a = malloc(5 * sizeof(int));
*
* for (int i = 0; i < 5; i++) {
* *(a + i) = i + 1;
* }
*
* return a;
* }
*
/
int countingSort(int arr_count, int* arr, int* result_count) {
// Allocate memory for the frequency array
int* frequency_array = (int*)malloc(100 * sizeof(int));
if (frequency_array == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return NULL; // Return NULL if memory allocation fails
}
// Initialize the frequency array to zeros
for (int i = 0; i < 100; i++) {
frequency_array[i] = 0;
}
// Count the occurrences of each element in the input array
for (int i = 0; i < arr_count; i++) {
int value = arr[i];
// Increment the count at the corresponding index
if (value >= 0 && value < 100) {
frequency_array[value]++;
}
}
// Set the result count to the size of the frequency array (always 100)
*result_count = 100;
// Return the frequency array
return frequency_array;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
int n = parse_int(ltrim(rtrim(readline())));
char** arr_temp = split_string(rtrim(readline()));
int* arr = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
int arr_item = parse_int(*(arr_temp + i));
*(arr + i) = arr_item;
}
int result_count;
int* result = countingSort(n, arr, &result_count);
for (int i = 0; i < result_count; i++) {
fprintf(fptr, "%d", *(result + i));
if (i != result_count - 1) {
fprintf(fptr, " ");
}
}
fprintf(fptr, "\n");
fclose(fptr);
return 0;
Counting Sort 1
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
include
include
include
include
include
include
char* readline(); char* ltrim(char*); char* rtrim(char*); char** split_string(char*);
int parse_int(char*);
/* * Complete the 'countingSort' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts INTEGER_ARRAY arr as parameter. */
/* * To return the integer array from the function, you should: * - Store the size of the array to be returned in the result_count variable * - Allocate the array statically or dynamically * * For example, * int* return_integer_array_using_static_allocation(int* result_count) { * *result_count = 5; * * static int a[5] = {1, 2, 3, 4, 5}; * * return a; * } * * int* return_integer_array_using_dynamic_allocation(int* result_count) { * *result_count = 5; * * int *a = malloc(5 * sizeof(int)); * * for (int i = 0; i < 5; i++) { * *(a + i) = i + 1; * } * * return a; * } * / int countingSort(int arr_count, int* arr, int* result_count) { // Allocate memory for the frequency array int* frequency_array = (int*)malloc(100 * sizeof(int)); if (frequency_array == NULL) { fprintf(stderr, "Memory allocation failed\n"); return NULL; // Return NULL if memory allocation fails }
}
int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
}
char* readline() { size_t alloc_length = 1024; size_t data_length = 0;
}
char* ltrim(char* str) { if (!str) { return '\0'; }
}
char* rtrim(char* str) { if (!str) { return '\0'; }
}
char** split_string(char* str) { char** splits = NULL; char* token = strtok(str, " ");
}
int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10);
}