title: quickSort array of structures: incorrect sort
body: <p>I'm having a problem with the following VS 2010 code.  Am trying to sort an array of structures.  The code compiles without errors, is very fast, but has a problem in that the sort result is incorrect.</p>
  
  <p>I am sorting by the "zip" character string (for test purposes, NOT numerically, but by character compare).  I have a version of this running using the standard lib qsort, but want to do some further fiddling so am writing my own.</p>
  
  <pre><code> struct address {
  
    char name[40];
    char street[40];
    char city[20];
    char state[30];
    char zip[21];
  };
  
  
  void qs_struct(struct address items[], int left, int right)
  {
  
    int i, j;
    char *x;
    struct address temp;
  
    i = left; 
    j = right;
    x = items[(left+right)/2].zip;
  
    do {
        while((strcmp(items[i].zip,x) &lt; 0) &amp;&amp; (i &lt; right)){ i++;}
        while((strcmp(items[j].zip,x) &gt; 0) &amp;&amp; (j &gt; left)) { j--;}
  
      if(i &lt;= j) {
        temp = items[i];
        items[i] = items[j];
        items[j] = temp;
        i++; j--;
      }
    } while(i &lt;= j);
  
    if(left &lt; j) qs_struct(items, left, j);
    if(i &lt; right) qs_struct(items, i, right);
  }
  
  
  void qx(struct address items[], int count)
  {
    qs_struct(items,0,count-1);
  }
  
  void fillStructWithRandomDataForTest(struct address *addr, int i, int j)
  {
      char temp[444];
  
  
              sprintf(temp, "%d%d", j +i, j*i);
              strcpy(addr-&gt;name, temp);
              sprintf(temp, "%d%d", j +i, j*i);
              strcpy(addr-&gt;street, temp);
  
              sprintf(temp, "%d%d", j +i, j*i);
              strcpy(addr-&gt;city, temp);
              sprintf(temp, "%d%d", j +i, j*i);
              strcpy(addr-&gt;state, temp);
  
              sprintf(temp, "%d%d", j +i, j*i);
              strcpy(addr-&gt;zip, temp);
  
  }
  
   void xqs(void)
    {
    struct address addrs[20];
  
     for (int i = 0, j = 33; i &lt; 16; ++i, --j)
          fillStructWithRandomDataForTest(&amp;addrs[i], i, j);
  
      qx(addrs, 16);
  
      // results: incorrectly sorted
      for (int k = 0; k &lt; 16; ++k)
          printf("%s \n",addrs[k].zip);
  
   }
  </code></pre>