title: about Quick Sort
body: <p>I have written this code but it will print these stack traces in the console please help me thanks! (Aslo "p" and "q" are the first and last index of our array ,respectively)</p>
  
  <pre><code>public class JavaQuickSort {
  
  public static void QuickSort(int A[], int p, int q) {
      int i, last = 0;
  
      Random rand = new Random();
      if (q &lt; 1) {
          return;
      }
      **swap(A, p, rand.nextInt() % (q+1));**
  
      for (i = p + 1; i &lt;= q; i++) {
          if (A[i] &lt; A[p]) {
              swap(A, ++last, i);
          }
  
      }
      swap(A, p, last);
      QuickSort(A, p, last - 1);
      QuickSort(A, last + 1, q);
  }
  
  private static void swap(int[] A, int i, int j) {
      int temp;
      temp = A[i];
      **A[i] = A[j];**
      A[j] = temp;
  
  
  }
  public static void main(String[] args){
      int[] A = {2,5,7,3,9,0,1,6,8};
      **QuickSort(A, 0,8 );**
      System.out.println(Arrays.toString(A));
  }
  }
  </code></pre>
  
  <p>the Stack traces :</p>
  
  <pre><code>run:
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -3
          at JavaQuickSort.swap(JavaQuickSort.java:38)
          at JavaQuickSort.QuickSort(JavaQuickSort.java:22)
          at JavaQuickSort.main(JavaQuickSort.java:45)
  Java Result: 1
  BUILD SUCCESSFUL (total time: 2 seconds)
  </code></pre>
  
  <p>I also bold those statements that cause these stack traces. like ==> ** ...**</p>
  
  <p>EDITED:</p>
  
  <pre><code>public class JavaQuickSort {
  
  public static void QuickSort(int arr[],int lo,int hi) {
      int n = arr.length;
      if(n&lt;=1)
          return;
      **int r = partition(arr);**
      **QuickSort(arr,lo , r-1);**
      QuickSort(arr, r+1, hi);
  }
  
  private static void swap(int[] A, int i, int j) {
      int temp;
      temp = A[i];
      **A[i] = A[j];**
      A[j] = temp;
  
  
  }
  public static int partition(int arr[]){
        int i, last = 0;
   Random rand = new Random();
      int n = arr.length;
      if (n &lt;= 1)
          return arr[0];
  
      **swap(arr, 0, rand.nextInt(n));**
  
      for (i =1; i &lt;n; i++) {
          if (arr[i] &lt; arr[0]) {
              swap(arr, ++last, i);
          }
  
      }
      swap(arr, 0, last);
      return last;
  }
  public static void main(String[] args){
      int[] A = {2,5,7,3,9,0,1,6,8};
      **QuickSort(A, 0,8 );**
      System.out.println(Arrays.toString(A));
  }
  }
  </code></pre>
  
  <p>I have edited my post for being more understandable also it will print these stack traces and I bold the lines that cause these stack traces!!!</p>
  
  <p>the stack traces :</p>
  
  <pre><code>run:
  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
          at JavaQuickSort.swap(JavaQuickSort.java:27)
          at JavaQuickSort.partition(JavaQuickSort.java:39)
          at JavaQuickSort.QuickSort(JavaQuickSort.java:19)
          at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
          at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
          at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
          at JavaQuickSort.QuickSort(JavaQuickSort.java:20)
          at JavaQuickSort.main(JavaQuickSort.java:52)
  Java Result: 1
  BUILD SUCCESSFUL (total time: 2 seconds)
  </code></pre>
  
  <p>please help me thanks</p>
  
  <p>EDITED :</p>
  
  <p>I have written this code with paying attention to the forst answer of this post but it wont sort my array!!!</p>
  
  <pre><code>public class JavaQuickSort {
  
  public static void QuickSort(int arr[], int lo, int hi) {
      if (hi &gt; lo) {
           Random rand = new Random();
          int pivotIndex = lo + rand.nextInt(hi-lo+1);
          int pivotnewIndex = partition(arr, lo, hi,pivotIndex);
          QuickSort(arr, lo, pivotnewIndex - 1);
          QuickSort(arr, pivotnewIndex + 1, hi);
      }
  }
  
  private static void swap(int[] A, int i, int j) {
      int temp;
      temp = A[i];
      A[i] = A[j];
      A[j] = temp;
  
  
  }
  
  public static int partition(int arr[],int lo,int hi,int pivotIndex)
  {
      int pivotValue = arr[pivotIndex];
      swap(arr, hi, pivotIndex);
      int storeIndex = lo;
      for(int i = lo;i&lt;hi;i++){
          if (arr[i]&lt;=pivotValue)
          swap(arr, storeIndex, i);
          storeIndex = storeIndex ++;
      }
      swap(arr, storeIndex, hi);
      return storeIndex;
  
  }
  
  public static void main(String[] args) {
      int[] A = {2, 5, 7, 3, 9, 0, 1, 6, 8};
      QuickSort(A, 0, 8);
      System.out.println(Arrays.toString(A));
  }
  }
  </code></pre>
  
  <p>the output:</p>
  
  <pre><code>run:
  [2, 9, 3, 8, 0, 6, 7, 1, 5]
  BUILD SUCCESSFUL (total time: 2 seconds)
  </code></pre>
  
  <p>I need your help really I confused !!!</p>