title: Quick Sort C++ Implementation (using random pivot) breaks and stuck in infinite loop
body: <p>I am trying to translate my Python Quick Sort implementation into C++ and have written some code. The code seems to break. I am using Xcode for development and all I see, when I compile &amp; run the code is <code>(lldb)</code> and the program seems to be stuck in infinite loop.</p>
  
  <p>Please help me find the bug and point out where am I going wrong in the C++ implementation.</p>
  
  <p><strong>Python Quick Sort Code:</strong></p>
  
  <pre><code>from random import randint
  
  def quickSort(lst):
      if not lst:
          return []
      else:
          pivot_index = randint(0, len(lst) - 1)
          pivot = lst[pivot_index]
          lesser = quickSort([l for i, l in enumerate(lst)
                              if l &lt;= pivot and i != pivot_index])
          greater = quickSort([l for l in lst if l &gt; pivot])
          #print lesser, pivot, greater
          return lesser + [pivot] + greater
  
  print quickSort([3, 2, 5, 6, 1, 7, 2, 4, 234, 234, 23, 1234, 24, 132])
  </code></pre>
  
  <p><strong>C++ Quick Sort Code:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  // simple print function 
  void print(int* lesser, int len_lesser, int pivot, int* greater, int len_greater) {
      for (int i=0; i &lt; len_lesser-1; i++) {
          cout &lt;&lt; lesser[i] &lt;&lt; " ";
      }
      cout &lt;&lt; pivot &lt;&lt; " ";
      for (int i=0; i &lt; len_greater-1; i++) {
          cout &lt;&lt; greater[i] &lt;&lt; " ";
      }
      cout &lt;&lt; endl;
  }
  
  unsigned long long rdtsc(){
      unsigned int lo,hi;
      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
      return ((unsigned long long)hi &lt;&lt; 32) | lo;
  }
  
  int randint(int len) {
      // srand(time(0));
      srand((unsigned)rdtsc());
      return (rand() % (len) + 0);
  }
  void quickSort(int* lst, int len) {
      if (lst == NULL) {
          cout &lt;&lt; "List Empty" &lt;&lt; endl;
          return;
      }
      else {
          int lesser[] = {};
          int greater[] ={};
          int j = 0;
          int k = 0;
          int pivot_index = randint(len);
          int pivot = lst[pivot_index];
          //cout &lt;&lt; pivot &lt;&lt; endl;
          for (int i=0; i &lt; len-1; i++) {
              if (lst[i] &lt;= pivot &amp;&amp; i != pivot_index) {
                  lesser[j] = lst[i];
                  j = j+1;
              }
          }
          int len_lesser = sizeof(lesser)/sizeof(int);
          quickSort(lesser, len_lesser);
  
          for (int i=0; i &lt; len-1; i++) {
              if (lst[i] &gt; pivot) {
                  greater[j] = lst[i];
                  k = k+1;
              }
          }
          int len_greater = sizeof(greater)/sizeof(int);
          quickSort(greater, len_greater);
          print(lesser, len_lesser, pivot, greater, len_greater);
      }
  
  }
  
  
  int main() {
      int lst[] = {3, 2, 5, 6, 1, 7, 2, 4, 234, 234, 23, 1234, 24, 132};
      int len = sizeof(lst)/sizeof(int);
      quickSort(lst, len);
      return 0;
  }
  </code></pre>