title: Stack OverFlow Exception in Quicksort Algorithm
body: <p>I am having a problem with my quicksort algorithm when trying to sort relatively big list that has no duplicates and are almost sorted (with only 5 numbers to sort). </p>
  
  <p>Below please find the code.</p>
  
  <pre><code> public static void MyQuickSort(List&lt;int&gt; list, int left, int right)
      {
          if (list == null || list.Count &lt;= 1)
              return;
  
          if (left &lt; right)
          {
              int pivotIdx = MyPartition(list, left, right);
              //Console.WriteLine("MQS " + left + " " + right);
              //DumpList(list);
              MyQuickSort(list, left, pivotIdx - 1);
              MyQuickSort(list, pivotIdx, right);
          }
      }
  
  static int MyPartition(List&lt;int&gt; list, int left, int right)
      {
          int start = left;
          int pivot = list[start];
          left++;
          right--;
  
          while (true)
          {
              while (left &lt;= right &amp;&amp; list[left] &lt;= pivot)
                  left++;
  
              while (left &lt;= right &amp;&amp; list[right] &gt; pivot)
                  right--;
  
              if (left &gt; right)
              {
                  list[start] = list[left - 1];
                  list[left - 1] = pivot;
  
                  return left;
              }
  
  
              int temp = list[left];
              list[left] = list[right];
              list[right] = temp;
  
          }
      }
  </code></pre>