Electrofriends

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

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();
}

Download exe and source code here.


Share and Enjoy:
  • Digg
  • Technorati
  • StumbleUpon
  • Twitter
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Mixx
  • Yahoo! Buzz
  • LinkedIn
  • NewsVine
  • RSS
  • email
Similar Posts:

21 Responses to “Insertion sort in C program”

  1. 1
    Hariharan Baskar Says:

    Hi,

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

  2. 2
    Gu3sss Says:

    Good explanation.. :)

  3. 3
    s gopi Says:

    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).

  4. 4
    Nirav dangi Says:

    realy, nice explanation….keep it up for other sorting methods…

  5. 5
    jamie Says:

    nice one! it’s understandable when demonstrations are being presented… Good job!!! =)

  6. 6
    bboy Says:

    c pseudocode is nt working

  7. 7
    Ali Shah Says:

    Fantastic article….great guys

  8. 8
    Prashanth Says:

    nice easy explanation on insertion sort………i liked it………good job

  9. 9
    harvey Says:

    enx in helping ne solve my problem in sorting it really work

  10. 10
    harvey Says:

    keep posting for me ,somehow learn more!!! thanks:)

  11. 11
    jay Says:

    hello, thank you for posting easy sample of insertion sort, now i iderstant it well.

  12. 12
    Jayashree Says:

    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 ……. :-)

  13. 13
    muthama Says:

    Thanks may God bless you ur great work

  14. 14
    kovalan Says:

    Thanks a lot keep this good work going.

  15. 15
    Anuj Says:

    very easy method …………….
    thanxs…….

  16. 16
    jahnavi Says:

    its understood easily. thanks for helping out

  17. 17
    Darshana Says:

    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 ………….!!!!

  18. 18
    Jayvee Says:

    Your the great

  19. 19
    Mohammed Nadeem Says:

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

  20. 20
    Kevin Says:

    Don’t you need to include conio.h?

  21. 21
    Kevin Says:

    Also, is Temp variable really necessary?

  22. 22
    arun Says:

    what is the work of this gotoxy(25,11+i); …..???

Leave a Reply

Share and enjoy

    • Digg
    • Facebook
    • Technorati
    • StumbleUpon
    • Twitter
    • Reddit
    • del.icio.us
    • Yahoo! Buzz
Copyright©2009 www.electrofriends.com All Rights Reserved. Powered by Dhyeya