Skip to main content

快速排序

代码源自该视频 算法思想:选择一个中心点,将比中心点小的移动到左边,反之移动到右边; 这时形成两个子序列,对子序列递归直至,每个序列只有一个元素为止;

为了省空间 设第一个元素为中心节点并存储该元素的值,从数组后面往前找一个 比中心节点小的放在第一个元素的位置上,然后再从前面往后找找一个元素,放在 后面空出来的元素的位置上。

当左右指针重合的时候,说明此时就是中心位置,故只需将最开始存储的第一个元素 放到该位置即可

时间复杂度 最好的情况是O(nlogn) 最差的情况是O(n²)

特点 如果基本有序 则会变成冒泡排序,时间复杂度为O(n²)

package whale.simpleAlgorithm;

/**
* @Author: WhaleFall541
* @Date: 2021/6/10 22:42
* @see <a href="https://www.bilibili.com/video/BV1nJ411V7bd?p=164">https://www.bilibili.com/video/BV1nJ411V7bd?p=164</a>
*/
public class QuickSort {
public static void main(String[] args) {
int[] arr = {-1, 20, -3, -10, 100, -255};

quickSort(arr, 0, arr.length - 1);
StringBuilder sb = new StringBuilder();
for (int i : arr)
sb.append(i).append(" ");
System.out.println("sb = " + sb);
}

private static void quickSort(int[] arr, int low, int high) {
if (low < high) {// 长度大于1
int position = moveBaseOnPivot(arr, low, high);
quickSort(arr, low, position - 1);
quickSort(arr, position + 1, high);
}
}

private static int moveBaseOnPivot(int[] arr, int low, int high) {
// {, 20, -3, -10, 100, -255};-1
int pivot = arr[low];
// 当low 和high相同时,说明中心点就在这里可以回填pivot元素了
while (low < high) {
// NOTE: 条件别忘了 low < high
// 当一直往前都没找到比pivot小的元素 从而造成数组越界
while (low < high && arr[high] >= pivot) high--;
arr[low] = arr[high];
while (low < high && arr[low] <= pivot) low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
}