//Program for insertion sort
#include<stdio.h>
#include<conio.h>
void insertion(int [], int size);
int main()
{
int a[30],i,size;
clrscr();
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf("Enter elements:");
for(i=0; i<size; i++)
{
scanf("%d",&a[i]);
}
insertion(a,size);
printf("\nSorted elements:\n\n");
for(i=0; i<size; i++)
printf(" %d",a[i]);
getch();
return 0;
}
void insertion(int a[], int n)
{
int i,j,tmp;
for(i=0;i<n;i++)
{
for(j=i-1;j>-1;j--)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
else
break;
}
}
}
/*Output of Program:
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>
void insertion(int [], int size);
int main()
{
int a[30],i,size;
clrscr();
printf("Enter total no. of elements : ");
scanf("%d",&size);
printf("Enter elements:");
for(i=0; i<size; i++)
{
scanf("%d",&a[i]);
}
insertion(a,size);
printf("\nSorted elements:\n\n");
for(i=0; i<size; i++)
printf(" %d",a[i]);
getch();
return 0;
}
void insertion(int a[], int n)
{
int i,j,tmp;
for(i=0;i<n;i++)
{
for(j=i-1;j>-1;j--)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
else
break;
}
}
}
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