title: Java Quicksort Partition method
body: <p>I am writing a Java quicksort method. my code currently is as follows</p>
  
  <pre><code>    public class Quicksort {
  
     public static void main(String[ ] args)
     {
        final String BLANKS = "  "; // A String of two blanks
        int i;                      // Array index
  
        int[ ] data = { 1000, 80, 10, 50, 70, 60, 90, 20, 30, 40, 0, -1000 };
  
        // Print the array before sorting:
        System.out.println("Here is the entire original array:");
        for (i = 0; i &lt; data.length; i++)
           System.out.print(data[i] + BLANKS);
        System.out.println( );
  
        // Sort the numbers, and print the result with two blanks after each number.
        quicksort(data, 1, data.length-2);
        System.out.println("I have sorted all but the first and last numbers.");
        System.out.println("The numbers are now:");
        for (i = 0; i &lt; data.length; i++)
           System.out.print(data[i] + BLANKS);
        System.out.println( );
     }
  </code></pre>
  
  <p>Quicksort Method       </p>
  
  <pre><code>    public static void quicksort(int[ ] data, int first, int n)
     {
        int pivotIndex; // Array index for the pivot element
        int n1;         // Number of elements before the pivot element
        int n2;         // Number of elements after the pivot element
  
        if (n &gt; 1)
        {
           // Partition the array, and set the pivot index.
           pivotIndex = partition(data, first, n);
  
           // Compute the sizes of the two pieces.
           n1 = pivotIndex - first;
           n2 = n - n1 - 1;
  
           // Recursive calls will now sort the two pieces.
           quicksort(data, first, n1);
           quicksort(data, pivotIndex + 1, n2);
        }
     }
  </code></pre>
  
  <p>Partition Method</p>
  
  <pre><code>   private static int partition(int[ ] data, int first, int n){ 
        int low = first;
        int high = n;
        int pivot = data[low];
  
        while(low &lt; high){
           low ++;
  
           while(low &lt;= high &amp;&amp; data[low] &lt; pivot){
              low ++;
           }
           while(high &gt;= low &amp;&amp; data[high] &gt; pivot){
              high--;
           }
           if(low&lt;=n &amp;&amp; low &lt; high){
              int temp = data[low];
              data[low] = data[high];
              data[high] = temp;
           }
        }
        return low;  
     }//end partition
  
  }//end class
  </code></pre>
  
  <p>When I currently run the program i get a result of 
  1000  80  0  10  70  60  90  20  30  40  50  -1000
  After several different attempts and rewrites of the partition method I still cannot get the array to sort properly. The task is to have the whole array sorted except the first and last numbers.</p>