title: C++ heap sort algorithm crashing after 4100+ data entries?
body: <p>I have developed the following heap sort algorithm code, but for some reason it works perfectly fine up until a certain (around the 4100 region), after which the program is forced to close?</p>
  
  <p>Any help would be greatly appreciated, thank you.</p>
  
  <pre><code>void heap_from_root(MVector &amp;v, int i, int n) {
  
      int end=n,j=0;
  
      // Identify the lowest root and how many sons it has. If it only has one son, set j=1.
      if (n==1) { n = 0; j = 1; }
      else if ((n-2) % 2 == 0) { n = (n-2)/2; }
      else if ((n-1) % 2 == 0) { n = (n-1)/2; j=1; }
  
      while (i &lt;= n) { // Start from the lowest root, and go up until the highest root.
  
          if (j==0) { // If 2 sons, then check for the biggest value. If the biggest value is greater than root, exchange.
  
              if (v[2*n+1] &gt; v[2*n+2] &amp;&amp; v[2*n+1] &gt; v[n]) { v.swap(2*n+1,n); }
              if (v[2*n+2] &gt; v[2*n+1] &amp;&amp; v[2*n+2] &gt; v[n]) { v.swap(2*n+2,n); }
  
          }
  
          if (j==1) { // If 1 son, if the son's value is greater than the root, exchange.
  
              if (v[2*n+1] &gt; v[n]) { v.swap(2*n+1,n); }
              j=0; // As only the last root can only have one son, set j to 0.
  
          }
  
          n--; // Go backwards to the next root.
  
      }
  
      /* The top value of the heap will now be the biggest value. If the heap hasn't been completed sorted, 
      put the biggest value at the END of the heap, and restart the algorithm, this time excluding that last 
      value. Repeating this recursively will mean the heap will be sorted in ascending order. This saves using
      significant excess memory. */
  
      if (i &lt; end) { v.swap(i,end); end--; heap_from_root(v,i,end); }
  
  }
  
  void heap(MVector &amp;v) { heap_from_root(v,0,v.size()-1); }
  </code></pre>
  
  <p>The MVector class is:</p>
  
  <pre><code>// class MVector contains arrays that can work with doubles
  class MVector
  {   
    // storage for the new vector class 
    vector&lt;double&gt; v;
    public:
      // constructor
      explicit MVector(){}
      explicit MVector(int n):v(n){srand(static_cast&lt;unsigned&gt;(time(NULL)));}
      explicit MVector(int n,double x):v(n,x){}
      // equate vectors;
      MVector&amp; operator=(const MVector&amp; X)
      {if(&amp;X==this)return *this;v=X.v;return *this;}
      // access data in vector 
      double&amp; operator[](int index){ return v[index]; }
      // access data in vector (const)
      double operator[](int index) const { return v[index]; }
      // size of vector
      int size() const {return v.size();}
      void push_back(double x){v.push_back(x);}
      void swap(int i,int j) { double c; c=v[i]; v[i] = v[j]; v[j] = c;  }
      void initialise_random(double xmin, double xmax) {
          const unsigned n = this-&gt;size();
          for(unsigned i=0;i&lt;n;i++) {
              v[i] = xmin + rand()*(xmax - xmin) / static_cast&lt;double&gt;(RAND_MAX);
          }
      }
  
      bool cmp(int i, int j) { 
          if (v[i] &lt; v[j]) { return true; } 
          else { return false; } 
      }
  }; // end class MVector
  </code></pre>
  
  <p>And its being initiated with</p>
  
  <pre><code>int main ()
  {
      MVector x(4500);
      x.initialise_random(0,10);
      double y0 = timer();
      Sort::heap(x);
      double y = timer();
  
      std::cout &lt;&lt; abs(y-y0) &lt;&lt; endl;
  }
  </code></pre>