我正在嘗試撰寫一個計數排序程式。我的問題是它似乎對前 4 個數字進行排序,然后他只列印 0。我的輸入來自外部檔案。
這是我的排序輸出:
0 1 5 8 0 0 0 0 0 0 0 0 0 0 0 0 0
以下是我到目前為止的代碼:
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;
}
}
uj5u.com熱心網友回復:
兩個周期 - 一個將計數設定為 0,另一個列印數字不應該在 len 之前,而是應該在MAX_VALUE.
for ( i = 0; i < MAX_VALUE; i )
{
count_array[i] = 0;
}
同樣在列印數字的函式中:
for (j = 0; j < MAX_VALUE; j )
如果沒有關于如何使用此代碼的更多背景關系,這是我發現的唯一問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/357618.html
上一篇:使用C中的指標將2個矩陣相乘
