#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int size, array[50], i, j, temp;
printf("Enter Array Size : ");
scanf("%d",&size);
printf("Enter Array Elements : ");
for(i=0; i<size; i++)
{
scanf("%d",&array[i]);
}
printf("Sorting array using Insertion sort ... \n");
for(i=1; i<size; i++)
{
temp=array[i];
j=i-1;
while((temp<array[j]) && (j>=0))
{
array[j+1]=array[j];
j=j-1;
}
array[j+1]=temp;
}
printf("Array after sorting : \n");
for(i=0; i<size; i++)
{
printf("%d ", array[i]);
}
getch();
}
Run:
Previous Next
#include<conio.h>
void main()
{
clrscr();
int size, array[50], i, j, temp;
printf("Enter Array Size : ");
scanf("%d",&size);
printf("Enter Array Elements : ");
for(i=0; i<size; i++)
{
scanf("%d",&array[i]);
}
printf("Sorting array using Insertion sort ... \n");
for(i=1; i<size; i++)
{
temp=array[i];
j=i-1;
while((temp<array[j]) && (j>=0))
{
array[j+1]=array[j];
j=j-1;
}
array[j+1]=temp;
}
printf("Array after sorting : \n");
for(i=0; i<size; i++)
{
printf("%d ", array[i]);
}
getch();
}
Run:
Previous Next
No comments:
Post a Comment