title: Quicksort doesn't work with sorted array
body: <p>I am doing a project to compare Bubblesort and Quicksort algorithms. Everything works fine until I want to sort data which has been already sorted with Quicksort method. It crashes on large arrays (50k, 100k).</p>
  
  <p>In my case firstly I sort data in descending order and then I try sorting in ascending order and then it crashes. </p>
  
  <pre><code>        read();  // just creates an array with random integers
          quickSortDSC(0, length - 1); //Here is the problem! (works if commented)
          t1 = clock();
          quickSortASC(0, length - 1);
          t2 = clock();
          cout &lt;&lt; "Quick Sort\t: " &lt;&lt; (t2 - t1)/CLK_TCK &lt;&lt; " sec\n";
  </code></pre>
  
  <p>Full code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  long length = 1000;
  const long max_length = 100000;
  
  int list[max_length];
  
  void read()
  {
      ifstream fin("random.dat", ios::binary);
      for (long i = 0; i &lt; length; i++)
      {
          fin.read((char*)&amp;list[i], sizeof(int));
      }
      fin.close();
  }
  
  void bubbleSort()
  {
      int temp;
      for(long i = 0; i &lt; length; i++)
      {
          for(long j = 0; j &lt; length-i-1; j++)
          {
              if (list[j] &gt; list[j+1])
              {
                  temp        = list[j];
                  list[j]     = list[j+1];
                  list[j+1]   = temp;
              }
          }
      }
  }
  
  
  long partitionASC(long left, long right)
  {
      int pivot_element = list[left];
      int lb = left, ub = right;
      int temp;
  
      while (left &lt; right)
      {
          while(list[left] &lt;= pivot_element)
              left++;
          while(list[right] &gt; pivot_element)
              right--;
          if (left &lt; right)
          {
              temp        = list[left];
              list[left]  = list[right];
              list[right] = temp;
          }
      }
      list[lb] = list[right];
      list[right] = pivot_element;
      return right;
  }
  
  long partitionDSC(long left, long right)
  {
      int pivot_element = list[left];
      int lb = left, ub = right;
      int temp;
  
      while (left &lt; right)
      {
          while(list[left] &gt;= pivot_element)
              left++;
          while(list[right] &lt; pivot_element)
              right--;
          if (left &lt; right)
          {
              temp        = list[left];
              list[left]  = list[right];
              list[right] = temp;
          }
      }
      list[lb] = list[right];
      list[right] = pivot_element;
      return right;
  }
  //ascending order
  void quickSortASC(long left, long right)
  {
      long pivot;
      if (left &lt; right)
      {
          pivot = partitionASC(left, right);
          quickSortASC(left, pivot-1);
          quickSortASC(pivot+1, right);
      }
  }
  
  //descending order
  void quickSortDSC(long left, long right)
  {
      long pivot;
      if (left &lt; right)
      {
          pivot = partitionDSC(left, right);
          quickSortDSC(left, pivot-1);
          quickSortDSC(pivot+1, right);
      }
  }
  
  int main()
  {
      double t1, t2;
  
      for (length = 1000; length &lt;= max_length; )
      {
          cout &lt;&lt; "\nLength\t: " &lt;&lt; length &lt;&lt; '\n';
  
          read();
          t1 = clock();
          bubbleSort();
          t2 = clock();
          cout &lt;&lt; "Bubble Sort\t: " &lt;&lt; (t2 - t1)/CLK_TCK &lt;&lt; " sec\n";
  
  
          read();
          quickSortDSC(0, length - 1); //Here is the problem!
          t1 = clock();
          quickSortASC(0, length - 1);
          t2 = clock();
          cout &lt;&lt; "Quick Sort\t: " &lt;&lt; (t2 - t1)/CLK_TCK &lt;&lt; " sec\n";
  
          if(length == max_length)
              break;
  
          switch (length)
          {
          case 1000 :
              length = 10000;
              break;
          case 10000 :
              length = 50000;
              break;
          case 50000 :
              length = 100000;
              break;
          }
      }
  
      return 0;
  }
  </code></pre>