package sorts;

/**
 * Created by wangzheng on 2018/10/16.
 */
public class QuickSort {

  // 快速排序，a是数组，n表示数组的大小
  public static void quickSort(int[] a, int n) {
    quickSortInternally(a, 0, n-1);
  }

  // 快速排序递归函数，p,r为下标
  private static void quickSortInternally(int[] a, int p, int r) {
    if (p >= r) return;

    int q = partition(a, p, r); // 获取分区点
    quickSortInternally(a, p, q-1);
    quickSortInternally(a, q+1, r);
  }

  private static int partition(int[] a, int p, int r) {
    int pivot = a[r];
    int i = p;
    for(int j = p; j < r; ++j) {
      if (a[j] < pivot) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
        ++i;
      }
    }

    int tmp = a[i];
    a[i] = a[r];
    a[r] = tmp;

    System.out.println("i=" + i);
    return i;
  }
}

@@ -575,24 +575,80 @@
  %3C pivot) %7B%0A
+        if (i == j) %7B%0A          ++i;%0A        %7D else %7B%0A  
         int 
@@ -663,27 +663,31 @@
 i%5D;%0A        
+  
 a%5Bi
+++
 %5D = a%5Bj%5D;%0A  
@@ -684,16 +684,18 @@
 = a%5Bj%5D;%0A
+  
         
@@ -710,28 +710,25 @@
 mp;%0A        
-++i;
+%7D
 %0A      %7D%0A   

update quick sort