程序员小灰-快速排序
挖坑法
public class QuickSort1 {
public static void main(String[] args) {
int[] arr = new int[] { 4, 7, 6, 5, 3, 2, 8, 1 };
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr, int startIndex, int endIndex) {
//递归结束条件:startIndex大等于endIndex的时候
if (startIndex >= endIndex) {
return;
}
//得到基准元素位置
int pivotIndex = partition(arr, startIndex, endIndex);
//用分治法递归数列的两部分
quickSort(arr, startIndex, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, endIndex);
}
private static int partition(int[] arr, int startIndex, int endIndex) {
// 取第一个位置的元素作为基准元素
int pivot = arr[startIndex];
int left = startIndex;
int right = endIndex;
//坑的位置,初始等于pivot的位置
int index = startIndex;
// 大循环在左右指针重合或者交错时结束
while (right >= left) {
// right指针从右向左进行比较
while (right >= left) {
if (arr[right] < pivot) {//如果右边元素比基准元素要小,需要填入目前的坑,同时新坑为右边刚刚的这个元素
arr[left] = arr[right]; //右边元素移动至左边
index = right; //新坑
left++; //左边指针右移
break;
}
right--;
}
// left指针从左向右进行比较
while (right >= left) {
if (arr[left] > pivot) {//如果左边元素比基准元素要大,需要填入目前的坑,同时新坑为左边刚刚的这个元素
arr[right] = arr[left]; //左边元素移动至右边
index = left; //左边为新坑
right--; //右边指针左移
break;
}
left++;
}
}
arr[index] = pivot;
return index;
}
}
指针交换法
public class QuickSort2 {
public static void main(String[] args) {
int[] arr = new int[] { 4, 7, 6, 5, 3, 2, 8, 1 };
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int [] arr, int startIndex, int endIndex) {
//递归结束条件:startIndex大等于endIndex的时候
if(startIndex >= endIndex) {
return;
}
//得到基准元素位置
int pivotIndex = partition(arr, startIndex, endIndex);
// 根据基准元素,分成两部分递归排序
quickSort(arr, startIndex, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, endIndex);
}
private static int partition( int [] arr, int startIndex, int endIndex) {
// 取第一个位置的元素作为基准元素
int pivot = arr[startIndex];
int left = startIndex;
int right = endIndex;
while(left != right) {
//控制right指针比较并左移
while (left pivot){
right--;
}
//控制right指针比较并右移
while( left
非递归实现
public class QuickSortWithStack {
public static void main(String[] args) {
int[] arr = new int[] { 4, 7, 6, 5, 3, 2, 8, 1 };
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr, int startIndex, int endIndex) {
// 用一个集合栈来代替递归的函数栈
Stack