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