title: Sorting a stack in ascending order in C, using two stacks
body: <p>I was given a task to sort numbers in stack <code>a</code> of integers in ascending order using two stacks <code>a</code> and <code>b</code>.</p>
  
  <p>Using eleven operations:</p>
  
  <ol>
  <li><strong>sa</strong> : <code>swap a</code> - swap the first 2 elements at the top of stack <code>a</code> </li>
  <li><strong>sb</strong> : <code>swap b</code> - swap the first 2 elements at the top of stack <code>b</code>.</li>
  <li><strong>ss</strong> : <code>sa</code> and <code>sb</code> at the same time.</li>
  <li><strong>pa</strong> : <code>push a</code> - take the first element at the top of <code>b</code> and put it at the top of <code>a</code>.</li>
  <li><strong>pb</strong> : <code>push b</code> - take the first element at the top of <code>a</code> and put it at the top of <code>b</code>.</li>
  <li><strong>ra</strong> : <code>rotate a</code> - shift up all elements of stack <code>a</code> by 1. The first element becomes
  the last one.</li>
  <li><strong>rb</strong> : <code>rotate b</code> - shift up all elements of stack <code>b</code> by 1. The first element becomes
  the last one.</li>
  <li><strong>rr</strong> : <code>ra</code> and <code>rb</code> at the same time.</li>
  <li><strong>rra</strong> : reverse <code>rotate a</code> - shift down all elements of stack <code>a</code> by 1. The last element
  becomes the first one.</li>
  <li><strong>rrb</strong> : reverse <code>rotate b</code> - shift down all elements of stack <code>b</code> by 1. The last element
  becomes the first one.</li>
  <li><strong>rrr</strong> : <code>rra</code> and <code>rrb</code> at the same time.</li>
  </ol>
  
  <p>My sorting function</p>
  
  <pre><code>void    sorts_stack(stack *a, stack *b)
  {
      int     srt;
  
      srt = is_not_sorted(a);
      if (srt)
      {
          if (a-&gt;list[srt] == top(a) &amp;&amp; a-&gt;list[srt] &gt; a-&gt;list[0])
          {
              rotate_ra_rb(a-&gt;list, a-&gt;size); //ra : rotate a
              putstr("ra\n");
          }
          else if (a-&gt;list[srt] == top(a) &amp;&amp; a-&gt;list[srt] &gt; a-&gt;list[srt - 1])
          {
              swap_sa_sb(a-&gt;list, a-&gt;size);//sa : swap a
              putstr("sa\n");
          }
          else if (a-&gt;list[srt] &gt; a-&gt;list[srt - 1])
          {
              putstr("pb\n"); //pb : push b
              push_pb(a, b);
          }
          sorts_stack(a, b);
      }
      else if (b-&gt;size &gt; 0)
      {
          if (top(a) &lt; top(b))
          {
              push_pa(a, b); //pa : push a
              putstr("pa\n");
          }
          else if ((top(a) &gt; top(b)) &amp;&amp; b-&gt;size != 0)
          {
              push_pa(a, b); //pa : push a
              putstr("pa\n");
          }
          sorts_stack(a, b);
      }
  }
  </code></pre>
  
  <p>my function sort the stack, I think it takes too many steps to sort. I need suggestions or advice on how to make it sort the stack with less steps taken.
  <a href="http://code.geeksforgeeks.org/d0ujw1">complete online code</a></p>