在這裏我試圖在10個元素的數組上實現mergesort。在提供輸入[10] = {9,8,7,6,5,4,3,2,1,0}時,我得到輸出爲{0,0,1,1,0,4,4 ,4,3,2},而預期產出爲{0,1,2,3,4,5,6,7,8,9}。我在main中調用mergesort,l = 0,h = 9。爲什麼執行mergesort的代碼不能產生正確的結果?
void mergesort(int a[],int l,int h)
{
int c[10];
int m=(l+h)/2;
if(l<h)
{
mergesort(a,l,m); // Recursive call to sort first half
mergesort(a,m+1,h); // Recursive call to sort second half
}
int i,k=l,j=l;
while(k<=h) // Merging the first and second half of the array
{
if(a[j]<a[m+1])
{
c[k]=a[j];
k++;
j++;
}
else
{
c[k]=a[m+1];
k++;
m++;
}
}
for(i=l;i<=h;i++)
a[i]=c[i];
}
你是第一次打電話給你?請顯示完整的示例(也稱爲[SSCCE](http://sscce.org/))。 –
預期輸出是什麼? –
也許你可以嘗試調試它? –