F How to perform Insertion Sort in an integer array? | CodeTheta

How to perform Insertion Sort in an integer array?

April 17, 2014

/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#define MAX 20
void isort(int*, int);
void display(int*, int);
void input(int*, int);
void main()
{
int a[MAX], max;
printf("/* http://native-code.blogspot.com */ \n\n Enter the maximum range:\n");
scanf("%d", &max);
printf("\nEnter the elemnts:\n");
input(a, max);
printf("\nBefore sorting: ");
display(a, max);
isort(a, max);
printf("\nAfter sorting: ");
display(a, max);
getch();
}
void input(int a[], int max)
{
int i;
for(i=0; i<max; i++)
scanf("%d", &a[i]);
}
void display(int a[], int max)
{
int i;
for(i=0; i<max; i++)
printf("%d ", a[i]);
}
void isort(int a[], int max)
{
int i, j, k, temp;
i=j=k=temp=0;
for(i=1; i<max; 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;
}
}
}
}

/* http://native-code.blogspot.com */

Post a Comment