codescracker


c

C Program Insertion Sort



« Previous Program Next Program »


Insertion Sort in C

To sort an array using insertion sort technique in C programming, you have to ask to the user to enter the array size and array elements in random order, now start sorting the element of the array in ascending order using insertion sort technique

C Programming Code for Insertion Sort

Following C program ask to the user to enter array size and array element to sort the array using insertion sort technique, then display the sorted array on the screen:

/* C Program - Insertion Sort */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int size, arr[50], i, j, temp;
	printf("Enter Array Size : ");
	scanf("%d",&size);
	printf("Enter Array Elements : ");
	for(i=0; i<size; i++)
	{
		scanf("%d",&arr[i]);
	}
	printf("Sorting array using selection sort ... \n");
	for(i=1; i<size; i++)
	{
		temp=arr[i];
		j=i-1;
		while((temp<arr[j]) && (j>=0))
		{
			arr[j+1]=arr[j];
			j=j-1;
		}
		arr[j+1]=temp;
	}
	printf("Array after sorting : \n");
	for(i=0; i<size; i++)
	{
		printf("%d  ", arr[i]);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program insertion sort

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test