by Dinesh Thakur Category: Array

Program sorts the integers in ascending order by using insertion method. The process of sorting starts with picking the second element which is arranged with respect to the first in the specified order (ascending or descending order).

Then, the next, (i.e., third element) is picked and it is placed in specified order with respect to the first two elements. Similarly, the fourth, fifth and so on, till the nth element is picked and placed. The total number of comparisons is less than in the previous process. This is illustrated in Program.

Illustrates the method of sorting of an integer array by insertion method.

#include <stdio.h>

void main ()
{
  int Array[10] = { 10,7,4, 9 ,1, 3, 5,6, 8 ,2 };
  int n, m, A, k;
  clrscr();
  for ( n =1; n < 10; n++)
    {
       A = Array [n];
       for(m= n-1; m >=0&& Array[m]>A;)
         {
           Array[m+1] = Array[m];
           m -- ;
         }
         Array [m+1]= A;
         for (k=0; k< 10;k++)
         printf("%d " , Array[k]);
         printf ("\n");
     }
}

C Program sorting of an int array using Insertion Method



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles