我無法弄清楚它出了什麼問題。請幫我調試我的插入排序程序
如果輸入:4,56,5,2,則顯示的輸出爲:2,4,0,1304。
如果輸入:27,54,43,26,2然後所示的輸出是:2,26,0,1304,0
如果輸入:34,87,54,4,34然後輸出示出的是:4,34,0,1304,0
基本上,只有前兩個排序的nos被顯示在輸出和其他地方,對於任何一組輸入都顯示1304或0。
#include <conio.h>
#include <stdio.h>
void main()
{
int a[10],b[10];
int i,size,j,k;
clrscr();
printf("please tell how many nos you want to enter");
scanf("%d",&size);
printf("Enter the nos");
for (i=0;i<size;i++) scanf("%d",&a[i]);
b[0]=a[0];
//insertionSort algo ---->
for (j=1;j<size;j++)
{
for (k=j-1;k>=0;k--)
//handling comparision with b[0]
if (k==0&&(a[j]<b[0])) {
b[1]=b[0];
b[0]=a[j];
}
//handling comparison with b[1:size-1]
if (k>0&&(a[j]<b[k])) { b[k+1]=b[k]; }
if (k>=0&&(a[j]>=b[k])) { b[k+1]=b[k]; break; }
}
for (i=0;i<size;i++) printf("%d\n",b[i]);
getch();
}
有什麼預期的結果? –
預期結果是輸入的數字以升序排列顯示 – Pradeep
邏輯極端有缺陷。拿出一張紙,畫出你認爲這個程序應該如何工作。看起來你沒有先理解程序就開始編碼。 – Coeffect