C Program to sort elements using INSERTION SORT

C Program for INSERTION SORT: C programming code for INSERTION SORT to sort numbers or arrange them in ascending order.

 

#include <stdio.h>
#include <conio.h>

int main()
{
int i,k,n,ptr,arr[20],temp;
printf(“****** Insertion Sort *******nn”);
printf(“Enter the size of array : “);
scanf(“%d”,&n);
arr[0]=-30,000;
printf(“nEnter the elements : n”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&arr[i]);

}

printf(“nOriginal Array : “);

for(i=1;i<=n;i++)
{
printf(“%dt”,arr[i]);
}

for(k=2;k<=n;k++)
{
temp=arr[k];
ptr=k-1;

while(temp<arr[ptr])
{
arr[ptr+1]=arr[ptr];
ptr=ptr-1;
}
arr[ptr+1]=temp;

}

printf(“nnSorted Array : “);
for(i=1;i<=n;i++)
{
printf(“%dt”,arr[i]);

}
getch();

}

OUTPUT

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s