## 快速排序 QuickSort
* 空間復雜度:O(nLogN)
* 時間復雜度:O(nLogN)
## 思路描述
1. 先選擇一個基準點p,以基準點p做分區操作(將比p小的數放到p的左邊,比p大的數放到右邊)
2. 然后遞歸在左邊0到p-1的部分排序,sort(0,p-1)
3. 再遞歸排右邊部分sort(p+1,len)
4. 如果left>=right,則退出
如下圖:

## 例子
```
old:[13, 7, 4, 6, 9, 24]
partition: old=[13, 7, 4, 6, 9, 24]
partition: left=0,right=5,value=4,j=0,swapCount=2 [4, 7, 13, 6, 9, 24]
partition: old=[7, 13, 6, 9, 24]
partition: left=1,right=5,value=6,j=1,swapCount=2 [6, 13, 7, 9, 24]
partition: old=[13, 7, 9, 24]
partition: left=2,right=5,value=7,j=2,swapCount=2 [7, 13, 9, 24]
partition: old=[4, 6]
partition: left=0,right=1,value=4,j=0,swapCount=2 [4, 6]
partition: old=[13, 9, 24]
partition: left=3,right=5,value=9,j=3,swapCount=2 [9, 13, 24]
partition: old=[4, 6, 7]
partition: left=0,right=2,value=6,j=1,swapCount=3 [4, 6, 7]
partition: old=[13, 24]
partition: left=4,right=5,value=13,j=4,swapCount=2 [13, 24]
partition: old=[4, 6, 7, 9]
partition: left=0,right=3,value=6,j=1,swapCount=3 [4, 6, 7, 9]
partition: old=[7, 9]
partition: left=2,right=3,value=7,j=2,swapCount=2 [7, 9]
partition: old=[4, 6]
partition: left=0,right=1,value=4,j=0,swapCount=2 [4, 6]
result:[4, 6, 7, 9, 13, 24]耗時:5ms
```
## java實現
~~~
public class QuickSort {
public static void main(String[] args) {
Stopwatch sw = Stopwatch.createStarted();
int[] nums = {13,7,4,6,9,24};
System.out.println("old:"+ Arrays.toString(nums));
System.out.println("result:"+Arrays.toString(sort(nums))+"耗時:"+sw.elapsed(TimeUnit.MILLISECONDS)+"ms");
}
public static int[] sort(int[] nums){
quickSort(nums,0,nums.length-1);
return nums;
}
// 快速排序
public static void quickSort(int[] nums,int left,int right){
if(left >= right){
return ;
}
// 基準坐標,分區
int p = partition(nums,left,right);
// 遞歸排左邊的
quickSort(nums,0,p-1);
// 遞歸排右邊的
quickSort(nums,p+1,right);
}
// 對arr[l...r]部分進行partition操作
// 返回index, 使得arr[l...p-1] < arr[p] ; arr[p+1...r] > arr[p]
private static int partition(int[] nums, int left, int right) {
// 基準值
int[] newNums = Arrays.copyOfRange(nums,left,right+1);
int p = (left + right) / 2;
// 將基準值和left交換
int swapCount = 2;
swap(nums, left, p);
int value = nums[left];
int j = left;
for (int i = left + 1; i <= right; i++){
if (nums[i] < value) {
j++;
swap(nums, j, i);
swapCount ++;
}
}
swap(nums, left, j);
System.out.println("partition: old="+Arrays.toString(newNums));
int[] sortNums = Arrays.copyOfRange(nums,left,right+1);
System.out.println("partition: left="+left+",right="+right+",value=" + value + ",j=" + j + ",swapCount=" + swapCount + " " + Arrays.toString(sortNums));
return j;
}
// 交換數組下標
public static void swap(int[] nums,int i,int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
~~~
- Redis來回摩擦
- redis的數據結構SDS和DICT
- redis的持久化和事件模型
- Java
- 從何而來之Java IO
- 發布Jar包到公共Maven倉庫
- Java本地方法調用
- 面試突擊
- Linux
- Nginx
- SpringBoot
- Springboot集成Actuator和SpringbootAdminServer監控
- SpringCloud
- Spring Cloud初識
- Spring Cloud的5大核心組件
- Spring Cloud的注冊中心
- Spring Cloud注冊中心之Eureka
- Spring Cloud注冊中心之Consul
- Spring Cloud注冊中心之Nacos
- Spring Cloud的負載均衡之Ribbon
- Spring Cloud的服務調用之Feign
- Spring Cloud的熔斷器
- Spring Cloud熔斷器之Hystrix
- Spring Cloud的熔斷器監控
- Spring Cloud的網關
- Spring Cloud的網關之Zuul
- Spring Cloud的配置中心
- Spring Cloud配置中心之Config Server
- Spring Cloud Config配置刷新
- Spring Cloud的鏈路跟蹤
- Spring Cloud的鏈路監控之Sleuth
- Spring Cloud的鏈路監控之Zipkin
- Spring Cloud集成Admin Server
- Docker
- docker日常基本使用
- docker-machine的基本使用
- Kubernetes
- kubernetes初識
- kubeadm安裝k8s集群
- minikube安裝k8s集群
- k8s的命令行管理工具
- k8s的web管理工具
- k8s的相關發行版
- k3s初識及安裝
- rancher的安裝及使用
- RaspberryPi
- 運維
- 域名證書更新
- 騰訊云主機組建內網
- IDEA插件開發
- 第一個IDEA插件hello ide開發
- 千呼萬喚始出來的IDEA筆記插件mdNote
- 大剛學算法
- 待整理
- 一些概念和知識點
- 位運算
- 數據結構
- 字符串和數組
- LC242-有效的字母異位詞
- 鏈表
- LC25-K個一組翻轉鏈表
- LC83-刪除有序單鏈表重復的元素
- 棧
- LC20-有效的括號
- 隊列
- 雙端隊列
- 優先隊列
- 樹
- 二叉樹
- 二叉樹的遍歷
- 二叉樹的遞歸序
- 二叉樹的前序遍歷(遞歸)
- 二叉樹的前序遍歷(非遞歸)
- 二叉樹的中序遍歷(遞歸)
- 二叉樹的中序遍歷(非遞歸)
- 二叉樹的后序遍歷(遞歸)
- 二叉樹的后序遍歷(非遞歸)
- 二叉樹的廣度優先遍歷(BFS)
- 平衡二叉樹
- 二叉搜索樹
- 滿二叉樹
- 完全二叉樹
- 二叉樹的打印(二維數組)
- 樹的序列化和反序列化
- 前綴樹
- 堆
- Java系統堆優先隊列
- 集合數組實現堆
- 圖
- 圖的定義
- 圖的存儲方式
- 圖的Java數據結構(鄰接表)
- 圖的表達方式及對應場景創建
- 圖的遍歷
- 圖的拓撲排序
- 圖的最小生成樹之Prim算法
- 圖的最小生成樹之Kruskal算法
- 圖的最小單元路徑之Dijkstra算法
- 位圖
- Java實現位圖
- 并查集
- Java實現并查集
- 滑動窗口
- 單調棧
- 排序
- 冒泡排序BubbleSort
- 選擇排序SelectSort
- 插入排序InsertSort
- 插入排序InsertXSort
- 歸并排序MergeSort
- 快速排序QuickSort
- 快速排序優化版QuickFastSort
- 堆排序HeapSort
- 哈希Hash
- 哈希函數
- guava中的hash函數
- hutool中的hash函數
- 哈希表實現
- Java之HashMap的實現
- Java之HashSet的實現
- 一致性哈希算法
- 經典問題
- 荷蘭國旗問題
- KMP算法
- Manacher算法
- Go