C Program for Insertion sort.

Insertion Sort :

#include <stdio.h>

#define MAX 10



void main()

{

	int a[MAX], i,n;

 printf("Enter size:");

 scanf("%d",&n);

 generate(a,n);

 printf("Random array:\n");

 for(i=0;i<n;i++)

    printf("%d ",a[i]);

	insertion_sort(a,n);



	printf("\nSorted elements:\n");

	display(a,n);

}



void insertion_sort(int a[],int n)

{



	int temp,i,j;

	for (i=0 ;i<n ;i++)

	{

		temp = a[i];

		j = i - 1;

		while (temp<a[j] && j>=0)

		{

			a[j + 1] = a[j];

			j = j - 1;

		}

		a[j + 1] = temp;

	}

}









int display(int a[],int n)

{

int i;

for(i=0;i<n;i++)

printf("%d ",a[i]);

}





void generate(int a[],int n)

{

int i;

for(i=0;i<n;i++)

   a[i]=rand()%100;

}

Post a Comment

0 Comments