Insertion sort in C program

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.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

194 thoughts on “Insertion sort in C program

  1. Hi,

    Really good article for which neatly explains the sort. Good work guys. keep posting 🙂

  2. This is not insertion sort, more like a selection sort.

    In insertion sort we have to move the elements to make room, but i understand that u have implemented that using the comparison and swapping of each preceding element, but since we are building a sorted list with each insertion, we don’t have to compare any elements that occur before the smaller element(see in the third iteration 88 >21 so u can stop the comparison there and move to the next element).

  3. hey…excelent ….it ws really confusing me a lot…bt, this example gave me a great idea of how it xactly works…
    thanks a lot ……. 🙂

  4. fantastic explanation and help for exam
    its actually thanks u and score t subject bcz f u guys…….!
    u r briliant*********
    very easy method…………
    and simple language& soft commnication between checker and examinar
    checker clear the concept……….bcz of me & u ………….!!!!

  5. hi , i’M Nadeem i checked your explaination and its really help full..
    Thousand thanks to your kind effort to help guys like me+

  6. whoops, seems some symbols are ignored as possible HTML tags.
    just check the while line then, and think about it

  7. Superb explanation.Thank you,
    But i have a question please any1 help me,
    if i understand logic of programs such like this then should i memorise it
    because it is hard to remember these logic and take alot of time if needed
    after few weeks without practise!

  8. hey friends,
    i need u r help to improve in c .
    well pls tel the how can i be best in this language
    waiting 4 a nice help line

  9. easily the best decsription of Insertion Sort on the web
    keep up the good work
    best of luck 🙂

  10. You just have to change the loop as for(i=(n-1);i>=0;i–)
    printf(“%d”,A[i]);
    in the last loop.

  11. its one of the good explain to know the basic of the insertion sort coding its help me to know about c

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in