sorting algorithm
  • Introduction
  • Wikipedia-sorting-algorithm
  • sorting algorithm
  • 历史 History
  • 分类 Classification
    • 稳定性 Stability
  • 算法比较 Comparison of algorithms
  • 流行排序算法 Popular sorting algorithms
    • 简单排序 Simple sorts
      • 插入排序 Insertion sort
      • 选择排序 Selection sort
    • 高效排序 Efficient sorts
      • 合并排序 Merge sort
      • 堆排序 Heapsort
      • 快速排序 Quicksort
    • 冒泡排序与变种 Bubble sort and variants
      • 冒泡排序 Bubble sort
      • 希尔排序 Shellsort
      • 梳排序 Comb sort
    • 分发排序 Distribution sort
      • 计数排序 Counting sort
      • 桶排序 Bucket sort
      • 基数排序 Radix sort
  • 内存使用模式和索引排序 Memory usage patterns and index sorting
  • 相关算法 Related algorithms
  • 另见 See also
  • 参考文献 References
  • 进一步阅读 Further reading
  • 外部链接 External links
Powered by GitBook
On this page
  • Quick sort快速排序
  • principle原理
  • example示例

Was this helpful?

  1. 流行排序算法 Popular sorting algorithms
  2. 高效排序 Efficient sorts

快速排序 Quicksort

Previous堆排序 HeapsortNext冒泡排序与变种 Bubble sort and variants

Last updated 5 years ago

Was this helpful?

Quick sort快速排序

@See @See - QuickSort

principle原理

把列表按中间值进行左右大小换位。 左边第一个比中间值大的,与右边自右向左第一个比中间值小的进行换位。

在根据左边循环后最大的位置为界,进行小范围的再按此方式循环置换,达到排序。

example示例

public void sort(int[] list) {
        if (list != null && list.length > 1) {
            quickSort(list, 0, list.length - 1);
        }
    }

    private void quickSort(int list[], int left, int right) {
        int index = partition(list, left, right);
        if (left < index - 1) {
            quickSort(list, left, index - 1);
        }
        if (index < right) {
            quickSort(list, index, right);
        }
    }

    /**
     * 分区折中交换
     *
     * @param list  int数组
     * @param left
     * @param right
     * @return
     */
    private int partition(int list[], int left, int right) {
        int i = left, j = right;
        int pivot = list[(left + right) / 2];//取折中点的值
        while (i <= j) {//如果是倒序,就是把以下两while条件判断修改
            while (list[i] < pivot)
                i++;
            while (list[j] > pivot)
                j--;
            if (i <= j) {
                swap(list, i, j);
                i++;
                j--;
            }
        }
        return i;
    }
https://en.wikipedia.org/wiki/Quick_sort
https://github.com/jiek2529/java_algorithm