My issue
this compiler is showing error message without any error in code which is specified in meg
My code
#include <stdio.h>
#include <stdlib.h>
void countingSort(int arr[], int size) {
if (size <= 0) return; // Handle empty or invalid input
// Find the maximum value in the array
int maxVal = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
// Create a count array of size maxVal + 1 and initialize to 0
int *count = (int *)calloc(maxVal + 1, sizeof(int));
// Count the occurrences of each value
for (int i = 0; i < size; i++) {
count[arr[i]]++;
}
// Update the original array with sorted values
int index = 0;
for (int i = 0; i <= maxVal; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
// Free the allocated memory for count array
free(count);
}
// Sample usage for testing the countingSort function
int main() {
int n = 8; // Example size
int arr[] = {6, 5, 3, 1, 8, 7, 2, 4}; // Example array
countingSort(arr, n);
// Print the sorted array
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Learning course: Design and analysis of Algorithms
Problem Link: https://www.codechef.com/learn/course/abesit-daa/ABESITDA23/problems/SESO45