Insertion sort in 2d arrays
<p>Using Borland Turbo C++, I made this program to demonstrate the insertion sort in a 2D array. The output is wrong and there seems to be a logical error which I am unable to find out. The insertion sort algorithm here is that it sorts all even rows in ascending order while all odd rows are sorted in descending order. Should the sort() function be broken down into evensort() and oddsort()? </p>

<pre><code>   #include&lt;iostream.h&gt;
   #include&lt;conio.h&gt;
   #include&lt;stdio.h&gt;

   int num[5][3];
   int i, j,r,c;

   void input(int r, int c)
   { cout&lt;&lt;"Enter the elements into the array"&lt;&lt;endl;
     for(i=0; i&lt;r; i++)
       for(j=0; j&lt;c;j++)
         cin&gt;&gt;num[i][j];
    }
   void sort(int r, int c)
     { for(i=0; i&lt;r; i++)
        { for(j=0; j&lt;c; j++)
           if(i%2==0)
           {  int min=num[i][j],pos=i;
 for(int k=i+1; k&lt;r; k++ )
  { if(num[k][j]&lt;min)
num[k][j]=min;
pos=k;
   }
 num[pos][j]=num[i][j];
 num[i][j]=min;
 }
else
{  int max=num[i][j],pos1=i;
 for(int l=i+1; l&lt;r; l++ )
  { if(num[l][j]&gt;max)
num[l][j]=max;
pos1=l;
   }
 num[pos1][j]=num[i][j];
 num[i][j]=max;
 }
}
}
 void display(int r, int c)
   { cout&lt;&lt;endl&lt;&lt;"Array  is"&lt;&lt;endl;
     for(i=0; i&lt;r; i++)
      { cout&lt;&lt;"\n";
        for(j=0; j&lt;c;j++)
         {
            cout&lt;&lt;num[i][j]&lt;&lt;"\t";
          }
    }
   }
 void main()
   { clrscr();
   cout&lt;&lt;"Enter the no of rows"&lt;&lt;endl;
   cin&gt;&gt;r;
   cout&lt;&lt;"Enter the no of columns"&lt;&lt;endl;
   cin&gt;&gt;c;
   if(r&lt;=5 &amp;&amp; c&lt;=3)
    { input(r,c);
     cout&lt;&lt;" Before sorting"&lt;&lt;endl;
     display(r,c);
     sort(r,c);
     cout&lt;&lt;"\n After sorting"&lt;&lt;endl;
     display(r,c);
    }
    else cout&lt;&lt;"Invalid no of rows and columns";
    getch();
    }
</code></pre>

<p>Output:</p>

<pre><code> Enter the number of rows
    4
   Enter the number of columns 
    3
   Enter the elements into the array
    1 2 3 4 5 6 7 8 9 10 11 12
    Array before sorting is
     1  2   3
     4  5   6
     7  8   9
     10 11 12
    Array after sorting is
    1  2  3
    4  5  6
    4  5  6
    4  5  6        
</code></pre>