Insertion sort in C program

Thursday, 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.

Avatar Image

Author Name :
Ranjith

Total : 154 Comments


154 Responses to “Insertion sort in C program”

  1. jamila says:

    yupeeeeeeeeeeeee,i got selection sort,thanks to u….

  2. jamila says:

    nice!but u people has to keep code in easy manner plz…

  3. jamila says:

    i want to learn insertion sort….

  4. Ranjit The Bandit says:

    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /* Configuration */

    static struct conv {
            int ch;
            const char *replace;
    } tab[] = {
            { ’<’,        "&lt;" },
            { ’>’,        "&gt;" },
            { ’ ’,        "&nbsp;" },
            { ’&’,        "&amp;" },
            { ’\"’,        "&quot;" },
            { 0,        0 }
    };

    static const char *tabstr = "&nbsp;";
    static int tabspaces = 8;
    static int usebreaks = 0;

    /* End of configuration */

    static int eol(int c, FILE *stream)
    {
            if (c == ’\n’)
                    return 1;
            if (c == ’\r’) {
                    if ((c = getc(stream)) != ’\n’)
                            ungetc(c, stream);
                    return 1;
            }
            return 0;
    }

    static const char *getstr(int c)
    {
            struct conv *t = tab;

            while (t->replace) {
                    if (c == t->ch)
                            return t->replace;
                    t++;
            }
            return 0;
    }

    static void tabify(FILE *dest, int n, const char *s)
    {
            while (n–)
                    fputs(s, dest);
    }

    static int htmlify(FILE *dest, FILE *src)
    {
            int c;

            while ((c = getc(src)) != EOF) {
                    const char *s;

                    if (usebreaks && eol(c, src)) {
                            fputs("<br />", dest);
                            continue;
                    }
                    if (c == ’\t’) {
                            tabify(dest, tabspaces, tabstr);
                            continue;
                    }
                    s = getstr(c);
                    if (s)
                            fputs(s, dest);
                    else
                            putc(c, dest);
            }
            return ferror(dest);
    }

    static const char *progname = "htmlify";

    int main(int argc, char *argv[])
    {
            FILE *stream;
            int retval;

            if (argv[0] && argv[0][0] != ”)
                    progname = argv[0];
            if (argc != 2) {
                    fprintf(stderr, "Usage: %s c-source-file\n", progname);
                    return EXIT_FAILURE;
            }
            stream = fopen(argv[1], "r");
            if (!stream) {
                    fprintf(stderr, "%s: %s: %s\n", progname, argv[1],
                            strerror(errno));
                    return EXIT_FAILURE;
            }
            retval = htmlify(stdout, stream) ? EXIT_FAILURE : 0;
            fclose(stream);
            return retval;
    }

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup