Insertion sort in C program
by Ranjith | December 4th, 2008.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
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 29 | #include<stdio.h> 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++) { gotoxy(25,11+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(); } |




















October 11th, 2009 at 11:08 pm
Hi,
Really good article for which neatly explains the sort. Good work guys. keep posting
October 15th, 2009 at 2:36 pm
Good explanation..
November 7th, 2009 at 8:16 pm
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).
November 10th, 2009 at 8:40 am
realy, nice explanation….keep it up for other sorting methods…
December 2nd, 2009 at 2:07 pm
nice one! it’s understandable when demonstrations are being presented… Good job!!! =)
December 29th, 2009 at 6:14 am
c pseudocode is nt working
December 29th, 2009 at 11:52 pm
Fantastic article….great guys
January 19th, 2010 at 11:37 pm
nice easy explanation on insertion sort………i liked it………good job
February 12th, 2010 at 5:06 am
enx in helping ne solve my problem in sorting it really work
February 12th, 2010 at 5:07 am
keep posting for me ,somehow learn more!!! thanks:)
March 11th, 2010 at 8:47 am
hello, thank you for posting easy sample of insertion sort, now i iderstant it well.
March 16th, 2010 at 8:03 pm
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 …….
March 19th, 2010 at 3:29 pm
Thanks may God bless you ur great work
March 23rd, 2010 at 12:09 pm
Thanks a lot keep this good work going.
April 2nd, 2010 at 12:19 pm
very easy method …………….
thanxs…….
April 6th, 2010 at 7:44 pm
its understood easily. thanks for helping out
April 9th, 2010 at 2:49 pm
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 ………….!!!!
June 16th, 2010 at 10:38 am
Your the great
June 24th, 2010 at 2:43 am
hi , i’M Nadeem i checked your explaination and its really help full..
Thousand thanks to your kind effort to help guys like me+
July 26th, 2010 at 11:52 pm
Don’t you need to include conio.h?
July 26th, 2010 at 11:59 pm
Also, is Temp variable really necessary?
July 28th, 2010 at 11:20 pm
what is the work of this gotoxy(25,11+i); …..???