Header Ads

Insertion Sort!

Insertion Sort Algorithm Pseudo-code & Source Code

Some times easy things easily arise confusions 😝 then I found a way through relationship to mitigate tricky confusion, so being respect 😊 of this algorithm, I just replace "i" to father (font_pointer) , "j" to son (back_pointer) & key or temp to datum.
(Datum is a singular form & Data is a plural form)
 

Algorithm OR Pseudo-code :
.
insertion_sort( values )
{
    for  father = 0  to  values.length - 1
        datum = values[ father ]
        son = father - 1
 
        while  son > 0  &  values[ son ] > datum
            values[ son + 1 ] = values[ son ]
            son = son - 1
 
        values[ son +]  = datum
}


Source Code :
#include <stdio.h>

void main() 
{
 int index, father , son , datum;

 printf("Please enter - array size : ");
 scanf( "%d" , &index );

 int values[ index ];
 printf("Please enter - data in array : ");
 for( father = 0 ; father < index ; father++ )
 {
  scanf( "%d" , &values[ father ] );
 }
 
 // Simply Print out Before Insertion
 printf("\nBefore Insertion : ");
 for( father = 0 ; father < index ; father++ )
 {
  printf( "%d " , values[ father ] );
 }

 //==================================================
 // Main Algorithume Accomplish Here :
 for( father = 1 ; father < index ; father++ )
 {
  datum = values[ father ];
  son = father - 1;

  while( son >= 0 && values[ son ] > datum )
  {
   values[ son + 1 ] = values[ son ];
   son = son - 1;
  }
  values[ son + 1 ] = datum; 
 }
 //==================================================

 // Simply Print out After Insertion
 printf("\nAfter  Insertion : ");
 for( father = 0 ; father < index ; father++ )
 {
  printf( "%d " , values[ father ] );
 }
 
 printf("\n\n");
 } 


 
ict link. Powered by Blogger.