title: failure in QVector<T>::operator[]: "index out of range" is thrown unexpectedly
body: <p>I have two sorting methods: insertion sort and shell sort. Two of those working function I have adapted to C++ from plain C. The problem is that ins_sort function works just well and shell_sort fails. What reason for that can be?</p>
  
  <pre><code>    bool less(QVector&lt;int&gt; &amp;arr, int a, int b)
      {
          return arr[a] &lt; arr[b];
      }
  
      // Performs swap on elements at a and b in QVector&lt;int&gt; arr
      void qswap(QVector&lt;int&gt; &amp;arr, int a, int b)
      {
          int temp = arr[a];
          arr[a] = arr[b];
          arr[b] = temp;
      }
  
      /* Failure is thrown in this method */
      void shell_sort(GraphicsView &amp;window, SwapManager &amp;manager)
      {
          auto list = window.items();
          QVector&lt;int&gt; arr;
          for (auto item : list)
              arr.push_back(static_cast&lt;QGraphicsRectWidget*&gt;(item)-&gt;m_number);
          int N = arr.size();
          int h = 1;
          while (h &lt; N/3) h = 3*h  + 1;
          while (h &gt;= 1)
          {
              for (int i = h; i &lt; N; ++i)
              {
                  for (int j = i; less(arr, j, j-h) &amp;&amp; j &gt;= h; j -= h)
                  {
                      qswap(arr, j, j-h);
                      manager.addPair(j, j - h);
                  }
              }
              h /= 3;
          }
      }
  </code></pre>
  
  <p>And that one does well.</p>
  
  <pre><code>    /* This method works just fine */
      void ins_sort(GraphicsView &amp;window, SwapManager &amp;manager)
      {
          auto list = window.items();
          int i, j;
          QVector&lt;int&gt; arr;
  
          for (auto item : list)
          {
              arr.push_back(static_cast&lt;QGraphicsRectWidget*&gt;(item)-&gt;m_number);
          }
          int N = arr.size();
          for (i = 1; i &lt; N; ++i)
          {
              for (j = i - 1; j != -1 &amp;&amp; less(arr, j + 1, j); --j)
              {
                  qswap(arr, j, j + 1);
                  manager.addPair(j, j + 1);
              }
          }
      }
  </code></pre>
  
  <p>Debugger points to this piece of code in "qvector.h"</p>
  
  <pre><code>    Q_ASSERT_X(i &gt;= 0 &amp;&amp; i &lt; d-&gt;size, "QVector&lt;T&gt;::operator[]", "index out of range");
        return data()[i]; }
  </code></pre>