我需要對數組進行排序,而這意味着輸入值,雖然用戶輸入的數字數組需要自行解決:我所做的到目前爲止是這樣的:數組排序同時輸入值
void MoveRight(int *a,int n, int startIndex)
{
int j,temp;
j=n-1;
for(int i=startIndex;i<n;i++)
{
temp = a[j];
a[j]=a[i];
a[i]=temp;
}
}
void InsertionSort(int *a,int n)
{
int i,number,j;
printf("Enter %d numbers for the array\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&number);
for(j=0;j<=i;j++)
{
if(number<a[j])
{
MoveRight(a,n,j);
a[j]=number;
break;
}
}
}
}
進入後:1,3,2,4,5
我得到的數組是:-858993460,-858993460,-858993460,-858993460,-858993460
什麼是'a'?發佈'InsertionSort'的調用 – LPs
數組'a'中的初始值是什麼?當你輸入1時,只有當數組中的任何元素大於1時,它纔會進入數組。在你的輸出中,數組包含小於'1'的-858993460,因此沒有值存儲在數組中 – Swanand
需要修改'InsertionSort'的算法 – Swanand