kikit
6/15/2016 - 7:35 AM

Insertion Sort

Insertion Sort

#include<stdio.h>
 
void InsertionSort(int a[], int n)
{
	int i,j, temp;
	for(i=0; i<=n-1; i++)
	{
		temp = a[i];
		j = i;
		while(a[j-1] > temp && j>=1)
		{
			a[j] = a[j-1];
			j--;
		}
		a[j] = temp;
	}	
}
 
 
int main()
{
	int n,i, array[20];
	printf("Enter total number of element : ");
	scanf("%i", &n);
	
	printf("Enter the element\n");
	for(i=0; i<n; i++)
	{
		scanf("%i", &array[i]);
	}
	
	InsertionSort(array,n);
 
	printf("sorted array : ");
	for(i=0; i<n; i++)
	{
		printf("%i ", array[i]);
	}
	
	getch();
	return 0;
}