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 #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com
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;
}
/*
* Program: InsertionSort.c
* Written by: iMaple
* Date:5, 25, 2012
* Comliler: vs2010
*/
#include
//Function Declaration
void InsertionSort(int a[], int Length);
int main(void)
{
int array[] = {21, 10, 15, 88, 95, 05};
InsertionSort(array, 6);
return 0;
}//main
void InsertionSort(int a[], int Length)
{
int counter;
int lookuper;
int temp;
for (counter = 1; counter 0&&a[lookuper - 1]>temp; lookuper–)
a[lookuper] = a[lookuper - 1];
a[lookuper] = temp;
}//for
printf(“\n\t The sorted list is ….”);
for (counter = 0; counter < 6; counter++)
printf("\n\n%2d\n", a[counter]);
return;
}//InsertionSort
its help alot! Thanxxx nd Keep posting…!!
nice explanation…!!
thanxx
samjle nahi……….. simple language madhe sangat ja….. jay maharashtra
void main()
{
int a[20],temp,j,i,n;
clrscr();
printf(“enter no. of elements\n”);
scanf(“%d”,&n);
printf(“enter elements\n”);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
for(j=1;ja[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();
}
simplest
very nice demo.
Hi
cod you hellp me with some tasks? ?
What is gotoxy….? Can u hlep me plz…
very nice demo
#include
#include
void selection(int at J,int n)
{
int i,j,min,loc;
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i+1;J<n.J++)
{
if(a[j]<min)
{min=a[J];
loc=J;
}
}
a[loc]=a[i];
a[i]=min
}
}
it helps me alot…tnx
void swap(int *src, int *dest)
{
int temp;
temp = *src;
*src = *dest;
*dest = temp;
}
main ()
{
int a[] = {4,8,7,3,2,9,0,1,6,5};
unsigned int n = sizeof(a)/sizeof(a[0]);
int i=0, j=0;
for(i=1; i=0; j–) {
if(a[j] > a[j+1]) {
swap(&a[j+1], &a[j]);
}
}
}
for(i=0; i<n; i++)
printf("Sorted list : %d ", a[i]);
return 0;
}
thankx sir ye program to hmara run hi nahi ho raha tha but now i done it
Its very helpful for me,now every confusion has been finished…………..so thank you so so …………..much.
Its very helpful for me,now every confusion has been cleared…………..so thank you so so …………..much.
just WOWWW…
Your diagram and code doestnt match!
Program is good, but not diagram.
#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++)
{
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=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();
}
thank u . . .Its is very useful . . .Keep posting more codes . .
i want print like this
.1
A B
2 3 4
C D E F . . . .like this any one help me write the coding for that
Sir. can you do a dutch flag problem using a insertion sort?
thanks
kya hai.. kuch output nai ara hai
simulation of sorting (all sorting)
simulation of sorting(all sorting) progam in c language
i need a unique & short program using insertion sort ?? can you help me ?
hii..i want to know the meaning of why we use j=j-1 after assigning temp to a[i] in insertion sort..please explain it as soon as possible my mail id is gchetna30@gmail.com.