# 5. 快速排序

nlogn

一句话总结：

> 选定一个 目标值
>
> 左边寻找比目标值大的，右边寻找比目标值小的，替换
>
> 最后左右轴相交，把左轴值和目标值对换，返回左轴索引
>
> 递归对每个二分区间执行同样操作

python

```python
def part_sort(array, left, right):
    tag = right
    key = array[tag]
    while left < right:
        # 左边位置，移到左边第一个比key大的位置
        while left < right and array[left] <= key:
            left += 1
        # 右边位置，移到右边第一个
        while left < right and array[right] >= key:
            right -= 1
        array[left], array[right] = array[right], array[left]
    # 因为先左边执行while，所以此时的arrar[left]一定是>=key的，因此arrar[left]可以和arrar[tag]交换，以满足左边对的不大于右边
    array[left], array[tag] = array[tag], array[left]
    return left


def quicksort(array, left, right):
    if left >= right: return
    stack = []
    stack.append([left, right])
    while len(stack) != 0:
        out = stack.pop()
        center = part_sort(array, out[0], out[1])
        if center - 1 > out[0]:
            stack.append([out[0], center - 1])
        if center + 1 < out[1]:
            stack.append([center + 1, out[1]])


if __name__ == '__main__':
    data = [16, 25, 39, 27, 12, 8, 45, 63]
    quicksort(data, 0, len(data) - 1)
    print(data)
```

java

```java
public abstract class Sort<T extends Comparable<T>> {
    public abstract void sort(T[] nums);

    public boolean less(T v,T w){
        return v.compareTo(w)<0;
    }

    public void swap(T[] a,int i,int j){
        T t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
}

public class QuickSort<T extends Comparable<T>> extends Sort<T> {
    @Override
    public void sort(T[] nums) {
        shuffle(nums);
        sort(nums,0,nums.length-1);
    }

    public void sort(T[] nums,int l,int h){
        if(l>=h) return;
        int j=partition(nums,l,h);
        sort(nums,l,j-1);
        sort(nums,j+1,h);
    }

    public int partition(T[] nums,int l,int h){
        int i=l,j=h+1;
        T v=nums[l];
        while(true){
            while(j!=l&&less(v,nums[--j]));
            while(i!=h&&less(nums[++i],v));
            if(i>=j) break;
            swap(nums,i,j);
        }
        swap(nums,l,j);
        return j;
    }

    public void shuffle(T[] nums){
        List<Comparable> list=Arrays.asList(nums);
        Collections.shuffle(list);
        list.toArray(nums);
    }

    public static void main(String[] args){
        Integer[] arr={
                5,1,8,7,10,6,9,5,20,3,0
        };
        Sort<Integer> sort=new QuickSort<>();
        sort.sort(arr);
        ArrayUtils.printArray(arr);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/pai-xu-lei/5-kuai-su-pai-xu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
