Insertion sort in C program

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

Here is the program to sort the given integer in ascending order using insertion sort method. Please find the pictorial tutor of the insertion sorting.

Logic : Here, sorting takes place by inserting a particular element at the appropriate position, that’s why the name-  insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.

If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like selection sorting and bubble sorting, which follows in the next pages.

Insertion Sort Demo
Insertion Sort Demo

C program for Insertion Sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
void main()
{
	int A[20], N, Temp, i, j;
	clrscr();
	printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
	scanf("%d", &N);
	printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
	for(i=0; i<N; i++)
	{
		scanf("\n\t\t%d", &A[i]);
	}
	for(i=1; 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;
	}
	printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
	for(i=0; i<N; i++)
		printf("\n\t\t\t%d", A[i]);
	getch();
}

Download exe and source code here.

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

189 Responses to “Insertion sort in C program”

  1. chetna goyal

    hii..i want to know the meaning of why we use j=j-1 after assigning temp to a[i] in insertion sort..please explain it as soon as possible my mail id is gchetna30@gmail.com.

    Reply
  2. karamjeet virk

    main()
    #define size 10
    {
    int arr[size],i,j,item;
    printf(“\n enter the element in array”);
    for(i=0;i<size;i++)
    scanf("%d",&arr[i]);
    for(j=0;j<size;j++)
    { item=arr[i];
    for(i=j-1;item=0;i–)
    { arr[i+1]=arr[i];
    arr[i]=item;
    }}

    Reply
  3. For balchhal programs contact AOT guys….West bengal…chodao bara

    Reply
  4. pls multy way tree sample program for ds insertion deletion plz............

    multy way tree

    Reply
  5. ATCHUTA RAO

    #include
    #include
    void main()
    {
    int a[20],k,n,i=0,j=0,t=0;

    clrscr();
    printf(“no of of elemts”);
    scanf(“%d”,&n);
    printf(“enter %d values”,n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    for(i=1;i=1;j–)
    {

    printf(“i=%d j=%d %d %d”,i,j,a[j],a[j-1]);
    if(a[j]>a[j-1])
    break;
    else
    {
    t=a[j];
    a[j]=a[j-1];
    a[j-1]=t;
    }

    } printf(“\n”);
    } printf(“\n”);

    for(i=0;i<n;i++)
    printf(" %d ",a[i]);
    getch();
    }

    THE ABOVE CODE REPRESENTS WHICH SORTING TECHNIQUE????

    DOES IT REPRESENT INSERTION SORT????

    Reply

Leave a Reply