I'm trying to write an Count sort Program. My Problem is that it seems to sort the first 4 Numbers and after that he only prints 0. My input comes from an external file.
This is my sorted output:
0 1 5 8 0 0 0 0 0 0 0 0 0 0 0 0 0
The following is my code so far:
int MAX_LAENGE = 1000;
int MAX_VALUE = 100;
int i, k, j;
void count_sort_calculate_counts(int input_array[], int len, int count_array[]) {
for ( i = 0; i < len; i )
{
count_array[i] = 0;
}
for (j = 0; j < len; j )
{
count_array[input_array[j]] = count_array[input_array[j]] 1;
}
}
void count_sort_write_output_array(int output_array[], int len, int count_array[]) {
k = 0;
for (j = 0; j < len; j )
{
for (i = 0; i < count_array[j]; i )
{
output_array[k] = j;
k = k 1;
}
}
CodePudding user response:
Two of the cycles - the one setting the counts to 0 and the one printing the numbers should not be until len, instead they should be until MAX_VALUE
.
for ( i = 0; i < MAX_VALUE; i )
{
count_array[i] = 0;
}
Similarly in the function that prints the numbers:
for (j = 0; j < MAX_VALUE; j )
Without having more context around how you use this code, this is the only issue I spot.