編寫一個程序,輸入一系列整數(在數組中存儲),然後通過調用函數selection_sort對整數進行排序。當給定一個包含n個元素的數組時,selection_sort必須執行以下操作: 1.搜索數組以找到最大值,然後將其移至最後位置。 2.遞歸調用排序數組的前n-1個元素。查找排序最大的函數問題
下面是我的代碼,我認爲的代碼是無處不在,我希望錯誤的一些高手可以幫我
#include <stdio.h>
int selection_sort(int a[])//this function have error that "i"and"count"is undeclared
{
int max = 0;
for (i = 1; i <= count; i++)// continuous compare to final
{
if (a[i] > max)
{
max=a[i];
}
}
a[count] = a[i]; //put the max to last position
count--;
}
int main(void)
{
int a[100] = { 0 },i=0,count=0;
while (1)
{
scanf("%d",&a[i]);
if (a[i] = '\n') { break; }//this line have error because '\n' not "int" so when i "enter" it would not break
i++;
count++; //counting how many integer i scanf
}
selection_sort();//call this function (i don't know well about function so i don't known where to put is correct)
return 0;
}
您的代碼有語法錯誤。 'selection_sort'如何看見'count'?另外,你的邏輯'a [i] =='\ n')'有缺陷。 'a [i]'永遠不會是''n''。另外,您正在'selection_sort'中以起始索引'1'訪問數組。那需要'0'。 – 2014-10-31 04:34:23
codeSmellHomework ++; //微笑 – Mawg 2014-11-14 13:32:33