Friday, May 21, 2010

This is a insertion sort program in c. i cant get the desired output. plz tell where is the mistake?

#include%26lt;stdio.h%26gt;


main()


{


int a[100],temp,i,j=0,n;


clrscr();


printf("enter the size of array");


scanf("%d",%26amp;n);


printf("enter the %d elements",n);


for(i=1;i%26lt;=n;i++)


scanf("%d",a[i]);


for(i=2;i%26lt;=n;i++)


{


temp=a[i];


j=i-1;


while((temp%26lt;a[j]) %26amp;%26amp; j!=0)


{


a[j+1]=a[j];


j=j-1;


}


a[j+1]=temp;


}


printf("the sorted array is\n");


for(i=1;i%26lt;=n;i++)


printf("%d\t",a[i]);


getch();


}


i get the output as 0 0 0 . plz tell where is the mistake.. thank u..

This is a insertion sort program in c. i cant get the desired output. plz tell where is the mistake?
Number of mistakes: 2





1. You need to check if "n" %26gt; 99, in that case your program will fail.





2. Fix the code as follows:


while((temp%26lt;a[j]) %26amp;%26amp; j!=0)


{


a[j+1]=a[j];


j=j-1;


a[j+1]=temp;


}


}


Notice that the statement a[j+1]=temp has been moved into the while loop,


No comments:

Post a Comment