当前位置:网站首页>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
边栏推荐
- 【颜值检测神器】来,请拿出你们的绝活(这颜值,对得起观众么?)
- LabVIEW或MAX下的VISA测试面板中串口无法工作
- 【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
- Redis installation and common problem solving based on centeros7 (explanation with pictures)
- Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
- 【 pygame Games 】 don't find, Leisure Games Theme come 丨 Bubble Dragon applet - - Leisure Games Development recommendation
- 【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
- LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046
- About optimizing API interface response speed
- Create millisecond time id in LabVIEW
猜你喜欢

How to generate automatic references (simple drawings)

干货丨MapReduce的工作流程是怎样的?

LabVIEW改变Point ROI Overlay的形状或颜色

LabVIEW obtains the information of all points found by the clamp function
![[pyGame games] don't look for it. Here comes the leisure game topic - bubble dragon widget - recommendation for leisure game research and development](/img/fb/a966b4bf52cdab4030578d4595e09b.png)
[pyGame games] don't look for it. Here comes the leisure game topic - bubble dragon widget - recommendation for leisure game research and development

Unity script cannot display Chinese comments of C # source code or the script created by vs does not have comments of C # source code

BGP - route map extension (explanation + configuration)

LabVIEW用VISA Read函数来读取USB中断数据

LabVIEW performs a serial loopback test

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
随机推荐
MySQL学习之子查询
LabVIEW改变Point ROI Overlay的形状或颜色
LabVIEW图片在从16位强制转换为8位后看起来要亮或暗
集合删除元素技巧 removeIf
LabVIEW中创建毫秒时间标识
Is it safe to open an account online in Shanghai?
BGP - route map extension (explanation + configuration)
Fiddler filtering sessions
OSS stores and exports related content
【数学】【连续介质力学】流体力学中的对称张量、应变张量和应力张量
关于优化API接口响应速度
[opencv practice] this seal "artifact" is awesome, saving time and improving efficiency. It is powerful ~ (complete source code attached)
VS的常用设置
Data and information resource sharing platform (VIII)
What is the workflow of dry goods MapReduce?
Unity 脚本无法显示C#源码的中文注释 或者VS创建的脚本没有C#源码的注释
Interface test learning notes
Redis installation and common problem solving based on centeros7 (explanation with pictures)
Four ways to add names to threads in the thread pool
Fiddler configuration