Search Your Query

Breaking

Friday, October 25, 2019

Insertion Sort In C



Insertion Sort In C


Code::

#include<stdio.h>
#include<stdlib.h>
void insertion(int *a,int n);
int main()
{
int *a,i=0,n,temp;
printf("Enter the size of array\n");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
printf("Enter the elements of array\n");
for(i=0;i<n;i++)
{
/* fflush(stdin);
printf("\n Enter %d th element-->",i);   */
scanf("%d\n",&a[i]);
}
insertion(a,n);
printf("The sorted array is-->\n");
for(i=0;i<n;i++)
{
printf("-->%d\n",a[i]);
}
return 0;
}
void insertion(int *a,int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0&&temp<a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
}

Hence: //coming soon

Input::

Enter the size of array
5
Enter the elements of array
5
4
3
2

1

Output::

The sorted array is-->
-->1
-->2
-->3
-->4
-->5


No comments:

Post a Comment