---
# System prepended metadata

title: 排序
tags: [Leetcode]

---

###### tags: `Leetcode`
# 排序
## 快速排序
:::spoiler 912 排序数组
### Java
```java=
class Solution {
    int n;
    public int[] sortArray(int[] nums) {
        n = nums.length;
        sort(nums,0,n-1);
        return nums;
    }
    public void sort(int[] nums,int left,int right){
       int store = left;
       if(left >= right)return;
       //以最右边的为基准，从最左边开始，小于基准的就
       for(int i = left; i < right; i++){
           if(nums[i] <= nums[right]){
               swap(nums,i,store);
               store++;
           }
       }
       swap(nums,store,right);//把基准和 nums[store] 交换
       //当前store左边的都比nums [store] 小，右边的都比 nums[store] 大。
       //store <= 0时，左边不继续。
       if(store>0)
       sort(nums,left,store-1);
       sort(nums,store,right);
    }
    
    public void swap(int []nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
```
:::