title: QuickSort with Random and median
body: <p>I am working on an assignment with QuickSort to show how fast the algorithm when using different methods for getting the Pivot like random or median of three. so far when using random or median I get different outputs and none of them is sorted, I couldn't figure out what my mistakes are. I went every where on the internet. Can someone looks at it and tell me what I'm doing wrong here?</p>
  
  <p>Here is the QuickSort code:</p>
  
  <pre><code>import java.util.*;
  public class QuickSort {
  
    public static void main(String[] args) { 
      int[] arr = {5, -32, 12, 43, 88, 19, 113, 62, -11, 2};
      System.out.println(Arrays.toString(arr));
      quickSort(arr);
      System.out.println(Arrays.toString(arr));
    }
  
    public static void quickSort(int[] arr) {
      quickSort1(arr, 0, arr.length -1);
    }
  
    private static void quickSort1(int[] list, int first, int last) {
      if (first &lt; last) {
        int pivotLocation = partition(list, first, last);
        quickSort1(list, first, pivotLocation - 1);
        quickSort1(list, pivotLocation + 1, last);
      }
    }
  
    private static int partition(int[] list, int first, int last) {
      int pivot;
      int smallIndex;
      Random rand = new Random();
      int num = rand.nextInt(list.length);
      swap(list, first, (first + last) / 2);   
      pivot = list[first];
      //pivot = medianOfThree(list, first, last);
      //pivot = list[num];
      smallIndex = first;
      for (int index = first + 1; index &lt;= last; index++) {
  
         if (list[index] &lt; pivot) {
            smallIndex++;
            swap(list, smallIndex, index);        // Should we limit to if(smallIndex != index)
         }
      }
      swap(list, first, smallIndex);
      return smallIndex;
   }
  
   private static void swap(int[] list, int first, int second) {
      int temp;
      temp = list[first];
      list[first] = list[second];
      list[second] = temp;
   } 
  
   private static int medianOfThree(int[] arr, int first, int last) {
      int f = arr[first], l = arr[last], m = arr[(last + first)/2];
      if(l &lt;= f &amp;&amp; l &gt;= m || l &gt;= f &amp;&amp; l &lt;= m)
        return l;
      else if (m &lt;= f &amp;&amp; m &gt;= l || m &gt;= f &amp;&amp; m &lt;= l)
        return m;
      return f;
   }
  
  }
  </code></pre>
  
  <p>I tried using <code>while()</code> it was faster but I have to test the speed of the sort with looping 100+ times which gave me<code>java.lang.StackOverflowError</code>.
  Any piece of advice will be helpful.</p>
  
  <p>Edit:
  I have fixed the median method and the random, thanks for the help.</p>
  
  <p>I was working on the while loop and I figured how to make it work and sort properly. The problem now is, whenever I try to make large array to test the speed of the sorting it gets stack and I'm not sure (by large I mean 10,000 elements).</p>
  
  <p>I call the class from another program but it's still not working as expected.</p>
  
  <p>here is the partition method, the class is the same:</p>
  
  <pre><code>private static int partition(int[] list, int first, int last) {
    Random rand = new Random();
  
    int pivot = 0;
    int num = first + rand.nextInt(last - first + 1);// generates random index 
    pivot = medianOfThree(list, first, last);        //finding median of three numbers
    //pivot = list[first];                           //using the first data as pivot
    //pivot = list[num];                            //Random index value is used as pivot
  
    int leftPointer= first  ;
    int rightPointer = last ;
    //swap(list, last, (first+last)/2);
    while(true) {
  
    while (list[leftPointer] &lt; pivot)
      leftPointer++;
    while (rightPointer &gt; 0 &amp;&amp; list[rightPointer] &gt; pivot)
      rightPointer--;
    if(leftPointer &gt;=rightPointer)
      break;
    swap(list, leftPointer, rightPointer);
    //count++;
    //System.out.println(Arrays.toString(list)+ "switch"+ count );
  }
  
  //System.out.println(Arrays.toString(list));
  //swap(list, last, leftPointer);
  //System.out.println(leftPointer);
  return leftPointer;
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>this is the Test code I'm using to test sorting efficiency  and the <code>QuickSort</code> using <code>while</code>loop is still not working as it should, am I doing something wrong?</p>
  
  <p>Test code:</p>
  
  <pre><code>import java.util.*;
  public class Test {
  
    public static final int ARRAYSIZE = 50000;        // Test array element count
    public static final int ELEMENTSIZE = 10000;      // Test array element size
    public static final int LOOPS = 1000;
  
    public static void main(String[] args) { 
      long t1=0,t2=0,t3=0;
      long c1=0,c2=0;  // Counters
      for(int test = 1; test &lt;= LOOPS; test++) {
       System.out.print(test + "..");
       Random rand = new Random();
       int[] arr1 = new int[ARRAYSIZE];
  
       for(int i = 0; i &lt; ARRAYSIZE; i++)             // Generate a random array with ARRAYSIZE elements
         arr1[i] = rand.nextInt(ELEMENTSIZE);        
  
       int[] arr2 = Arrays.copyOf(arr1, arr1.length); // Use an exact array copy for each sorting test
       int[] arr3 = Arrays.copyOf(arr1, arr1.length);
  
  
       t1 = System.currentTimeMillis();
       QuickSort.quickSort(arr1);                //Run &amp; Time Quick Sort
       t2 = System.currentTimeMillis();
       Arrays.sort(arr3);                         //Run &amp; Time Arrays.sort
       t3 = System.currentTimeMillis();
  
       c1 += t2-t1; 
       c2+=t3-t2; 
  
  
     }
      System.out.println();
      System.out.println("Quick Sort took: " + c1/LOOPS + " milliseconds");
      System.out.println("Arrays.sort took:    " + c2/LOOPS + " milliseconds");
  
    }
  
    /* ADD YOUR CODE HERE */
  
  }
  </code></pre>