当前位置:网站首页>Quick sort
Quick sort
2022-06-10 23:57:00 【Li_ XiaoJin】
About quick sort
The idea of quick sorting :
- Select a number from the array as the reference value , Usually choose the first number , Or the last number .
- Use double pointer ( Head and tail ) Traverse , Find the first number larger than the reference value from left to right , Find the first number smaller than the reference value from right to left , Swap two positions , Until the head and tail pointers are equal or the head pointer is greater than the tail pointer , Exchange the reference value with the number of the header pointer . After such a round , The number on the left is smaller than the reference value , The number on the right is larger than the reference value .
- For the left series , Repeat the above 1,2 step . Repeat for right 1,2 step .
- After the recursion of the left and right sequences , Sort complete .
https://lixj.fun/upload/2021/07/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F-eadb58e4707d48d6a047cecc1b637849.gif
Algorithm complexity :O(nlogn)
Algorithm space complexity :O(1)
Algorithm stability : unstable
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
int i,j,temp;
if (i > j) {
return;
}
i = low;
j = high;
temp = arr[low];
while (i < j) {
while (arr[j] >= temp && i < j) {
j--;
}
while (arr[i] <= temp && i < j) {
i++;
}
if (i < j) {
// In exchange for i and j Value
swap(arr, i, j);
}
}
// In exchange for low and i Value
swap(arr, low, i);
quickSort(arr, low, i-1);
quickSort(arr, i+1, high);
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args){
int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
// int[] arr = {6 ,1 ,2, 7 ,9 ,3 ,4 ,5 ,10 ,8};
quickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/ Quick sort
边栏推荐
- High speed data stream disk for LabVIEW
- LeetCode 501 :二叉搜索樹中的眾數
- SystemVerilog (x) - user defined type
- LabVIEW phase locked loop (PLL)
- Interface test learning notes
- LabVIEW确定控件在显示器坐标系中的位置
- Binary tree pruning
- Data and information resource sharing platform (VII)
- 【Pygame小游戏】“史上最炫酷贪吃蛇”驾到,FUN开玩(不好玩不要钱)
- Unity script cannot display Chinese comments of C # source code or the script created by vs does not have comments of C # source code
猜你喜欢

What Fiddler does for testing

B 树的简单认识

What is the workflow of dry goods MapReduce?

【Pygame小游戏】不怕你走不过系列:极致AI走迷宫,学习完带你打开新世界大门~(附游戏源码)

Dark horse headlines - Tencent's salary system reform has caused controversy; Intel expanded the recruitment of female engineers nationwide; Black horse is 100% employed really

【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)

BGP - route map extension (explanation + configuration)

Simple impedance matching circuit and formula

Create millisecond time id in LabVIEW

Six procurement challenges perplexing Enterprises
随机推荐
The serial port in the visa test panel under LabVIEW or max does not work
curl导入postman报错小记
LabVIEW phase locked loop (PLL)
Interview questions - written examination
Is it safe for CICC Fortune Securities to open an account? Is it reliable?
LabVIEW打开其他EXE程序
Basic introduction and core components of kubernetes
How to remove the blank at the top of listview
Multipartfile rename upload
Is it safe for changtou school to open an account? Is it reliable?
Unity 脚本无法显示C#源码的中文注释 或者VS创建的脚本没有C#源码的注释
LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行
判等问题:如何确定程序的判断是正确的?
Unity script cannot display Chinese comments of C # source code or the script created by vs does not have comments of C # source code
LabVIEW编程规范
After deepin20 menu startup option, the self-test indicates that iwlwwifi is stopped
Data and information resource sharing platform (V)
Hyperleger fabric installation
[latex] latex vs Code Snippets
easyrecovery15操作简单方便的数据恢复工具