/*write a c program to sort an array element using insertion sort method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,k,temp=0;
clrscr();
printf("how many number you want to enter:-");
scanf("%d",&n);
printf("\nenter number for an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[k+1]=temp;
}
}
}
printf("\nthe sorted array is:-\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
/*how many number you want to enter:-7
enter number for an array7
8
4
1
9
3
4
the sorted array is:-
1
3
4
4
7
8
9
*/
No comments:
Post a Comment