Insertion sorting in c language
Sorting is the process of arrenging data in ascending or decending order.
Like- dictionary
You think that if alphabets is not in sorted way how you can find any word.
To handle it we can sorted char,int.
THEORY-
in insertion sorting
each next element is compare all element behind it.
syntax-
#include<stdio.h>
int main()
{ int n,i,j,a[1000],temp;
printf("enter how many no you wanted to sort\n");
scanf("%d",&n);
printf("ENTER NO FOR SORTING\N");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
if(a[j]>temp)
{
a[j+1]=a[j];
}
else
break;
a[j+1]=temp;
}
printf("SORTED \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
exit
if you have any doubt please comment.and tell me what topic you want to learn.
Comments
Post a Comment