title: How can I fix my code? Quick-sort fill with random numbers
body: <p>I am writing a code using quicksort but can't seen to make it work.</p>
  
  <p>I have declared my array to size 20 and filled it with random integers. Then I called my <code>sort</code> method to do the quicksorting. The <code>show</code> method just prints the output. Still my code doesn't work at all.</p>
  
  <p>My entire code for quick-sorting:</p>
  
  <pre><code>public class JavaApplication3 {
      public static void main(String[] args) {
          QuickSort B = new QuickSort();
  
          int[] snum = new int [20];
          for(int index = 0; index &lt; snum.length; index++ ){
              snum[index]= (int) (Math.random ());
          } 
  
          B.sort(snum);
          B.show(snum);     
      }
  }
  
  class QuickSort{
      public static void sort(int [] a ){
          quicksort(a, 0, a.length -1);
      }
  
      private static void quicksort(int[] a, int lo, int hi){
          if(hi &lt;= lo) return;
          int j = partition(a, lo, hi);
          quicksort(a, lo, j-1);
          quicksort(a, j+1, hi);
      }
  
      private static void exch(int [] a, int i, int j){
          int t = a[i];
          a[i] = a[j];
          a[j] = t;
      }
  
      private static int partition(int[] a, int lo, int hi){
          int i= lo;
          int j = hi+1;
          int pivot = a[lo]; 
          while(i&lt;=j){
              while(a[i] &lt; pivot){
                  i++;
              }
              while(a[j] &gt; pivot){
                  j--;
              }
              if(i &lt;= j){
                  exch(a, i, j);
                  i++;
                  j--;
              }
          }
          if (lo &lt; j)
              quicksort(a, lo, j);
          if (i &lt; hi)
              exch(a, i, j);
  
         return j;
      }
  
      public static void show(int[]a){
          for(int i=0; i&lt;a.length;i++)
              System.out.print(a[i] + " ");
          System.out.println();
      }
  </code></pre>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
    20    at javaapplication3.QuickSort.partition(JavaApplication3.java:61)
      at javaapplication3.QuickSort.quicksort(JavaApplication3.java:43)   at
    javaapplication3.QuickSort.sort(JavaApplication3.java:39)     at
    javaapplication3.JavaApplication3.main(JavaApplication3.java:29) Java
    Result: 1</p>
  </blockquote>