//Program for insertion sort
#include<stdio.h>
#include<conio.h>
//Insertion fuction
void insertion(int a[10],int n)
{
int temp,i,j,k;
for(i=0;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;
}
}
}
}
//Main function
void main()
{
int a[10],i,n;
clrscr();
printf("Enter the array size:");
scanf("%d",&n);
printf("\nEnter the elements into array:");
for(i=0;i<n;i++)
{
scanf("%d\t",&a[i]);
}
printf("\nEntered array: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
insertion(a,n);
printf("\n\nSorted list: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
//end of main
/*Output
Enter the array size:6
Enter the elements into array:31 56 80 111 65 71
Entered array: 31 56 80 111 65 71
Sorted list: 31 56 65 71 80 111 */
#include<stdio.h>
#include<conio.h>
//Insertion fuction
void insertion(int a[10],int n)
{
int temp,i,j,k;
for(i=0;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;
}
}
}
}
//Main function
void main()
{
int a[10],i,n;
clrscr();
printf("Enter the array size:");
scanf("%d",&n);
printf("\nEnter the elements into array:");
for(i=0;i<n;i++)
{
scanf("%d\t",&a[i]);
}
printf("\nEntered array: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
insertion(a,n);
printf("\n\nSorted list: ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
//end of main
/*Output
Enter the array size:6
Enter the elements into array:31 56 80 111 65 71
Entered array: 31 56 80 111 65 71
Sorted list: 31 56 65 71 80 111 */
Pankaj Gaikar is a
professional blogger
from Pune, India who writes on Technology, Android,
Gadgets, social media and latest tech updates at
Punk Tech
,
Being Android
&
Shake The Tech
. Email me
HERE
0 comments :
Post a Comment