# 理解原理

.

.

.

## 挖坑法代码


public class QuickSort {

public static void main(String[] args) {

int[] arrays = {12,21,3,5,2,18,7,4,11,9,12};
System.out.println("快速排序之挖坑排序前的数组:" +Arrays.toString(arrays));
pothlingSort(arrays, 0 , arrays.length - 1);
System.out.println("快速排序之挖坑排序后的数组:" +Arrays.toString(arrays));
}

/**
* 挖坑法
* @param arrays
* @param low
* @param high
*/
public static void pothlingSort(int[] arrays , int low , int high){

if(low < high){

//求每次分治的分割线
int divideIndex = getDivideIndex(arrays,low,high);
//再递归分别对分割的俩个子数组进行递归排序
pothlingSort(arrays,low,divideIndex -1);
pothlingSort(arrays,divideIndex + 1, high);
}

}

private static int getDivideIndex(int[] arrays, int low, int high) {
int pivot = array[low];
int left = low;
int right = hight;
int index = low;

while (right >= left){

while(right >= left){
if (array[right] < pivot){
array[left] = array[right];
index = right;
left++;
break;
}
right--;
}

while(right >= left){
if(array[left] > pivot){
array[right] = array[left];
index = left;
right --;
break;
}
left++;
}

}
}


## 2.指针交换法

.

.

.

package com.Jevin.priorityQueue;

public class QuickSort {

public static void quickSort(int[] arr,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
//temp就是基准位
temp = arr[low];

while (i<j) {
//先看右边，依次往左递减
while (temp<=arr[j]&&i<j) {
j--;
}
//再看左边，依次往右递增
while (temp>=arr[i]&&i<j) {
i++;
}
//如果满足条件则交换
if (i<j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}

}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = temp;
//递归调用左半数组
quickSort(arr, low, j-1);
//递归调用右半数组
quickSort(arr, j+1, high);
}

public static void main(String[] args){
int[] arr = {7,10,2,4,7,1,8,5,19};
quickSort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}

05-17 1053

06-18 301
08-14 9942
07-01 7万+
06-04 8111
03-22 1515
04-11 396
12-27 1061
07-10 872
08-01 1万+
07-20 3722
06-10 58
02-10 8879
06-05 3万+
09-05 499
09-29