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(); } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
yupeeeeeeeeeeeee,i got selection sort,thanks to u….
nice!but u people has to keep code in easy manner plz…
i want to learn insertion sort….
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Configuration */
static struct conv {
int ch;
const char *replace;
} tab[] = {
{ ’<’, "<" },
{ ’>’, ">" },
{ ’ ’, " " },
{ ’&’, "&" },
{ ’\"’, """ },
{ 0, 0 }
};
static const char *tabstr = " ";
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;
}