img/home.png

Insertion Sort


This is the Insertion sort written in our dialect of the C language:


$DECLAREINT j i jm t zero one n temp temp1 temp2
$DIMENSION tab[20]
$RINVAR tab[](-25,25) n(1,10)
$RETVAR tab[]

zero=0;
one=1;
for (i=zero;i<n;i++) {
    j=i;
    t=zero;
    if (j>t) {
       t=one;	
       jm=j;
       jm--;
       temp1=tab[jm];
       temp2=tab[j];
       if (temp1<=temp2) {
          t=zero;	
       }
       while (t>zero) {
             temp1 = tab[j];
             jm=j;
             jm--;
             temp2 = tab[jm];
             tab[j] = temp2;
             tab[jm] = temp1;
             j--;
             if (j==zero) {
             	 break;
             }
             jm=j;
             jm--;
             temp1=tab[jm];
             temp2=tab[j];
             if (temp1<=temp2) {
                t=zero;	
             }
       }
    }
}       	 

And bellow is the Insertion Sort code, after it has been automatically refactored by the Critticall! Slick, short and elegant, with no unnecessary table readings, but still the Insertion, no doubt.



$DECLAREINT i n jm temp2 temp1 one zero 
$DIMENSION tab[20]
$RINVAR tab[](-25,25) n(1,10) 
$RETVAR tab[] 

// int tab[20]; int i=0;int n=0;int jm=0;int temp2=0;int temp1=0;int one=0;int zero=0;
one=0; jm=0; i=1; while (i<n) { temp1=tab[jm]; one=i; temp2=tab[one]; while (temp1>temp2) { tab[jm]=temp2; tab[one]=temp1; if (jm==zero) { break; } one=jm; jm--; temp1=tab[jm]; } jm=i; i++; }