当前位置:网站首页>Quick sorting of top ten sorting
Quick sorting of top ten sorting
2022-07-25 23:50:00 【Flowers bloom without knowing you】
Exchange method
The steps of exchange quick sorting
1、 Find a dividing point , Let the value on the left of the dividing point be less than the dividing point , The value on the right is greater than or equal to the dividing point .
2、 Do this on the left .
3、 Right, do it on the right .
Core code
# Incomplete code
private static int partition2(int[] arr, int left, int right) {
int i = left, j = right;
int pivot = arr[left];
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
while (i < j && arr[i] <= pivot) {
i++;
}
if (i < j) {
swap(arr, i, j);
}
}
swap(arr, left, i);
return i;
}
Q&A
Why go forward from the back , Can you change the position ?
1、 Because our chosen pacesetter is on the left , So we're going to traverse from the right
Then why? Because the pacesetter we choose is on the left , Just start on the right ?
Further analysis , We finally need to set a model i==j The value of the stopped position is exchanged , So where to stop , It must be smaller than the pacesetter , Otherwise, I will not be satisfied , The value on the left of the pacesetter is less than the premise of the pacesetter . Suppose we start on the left , Then there will be people who finally stop at a place larger than the pacesetter , It leads to the violation of the principles mentioned above when the exchange finally takes place . And from the right, we can guarantee that we must encounter Value smaller than the pacesetter or return to the pacesetter's position Just stop .
2、 If you want to start on the left , Then the pacesetter can choose the rightmost .
边栏推荐
- Learning exploration-3d rotation card
- Nacos 下线服务时报错 errCode: 500
- Duplicate numbers in array
- JS synchronization and asynchrony
- Problem set
- C language implementation of three chess
- 智牛股--09
- Get the data of Mafeng Hotel
- Wrote a little webapi knowledge points from 0 to 1
- Interview focus - TCP protocol of transport layer
猜你喜欢

initializer_ List tool library learning

Inheritance (the child constructor inherits the attributes in the parent constructor)
![[Muduo] EventLoop event cycle](/img/80/824c7061d58796d454be0c438e257c.png)
[Muduo] EventLoop event cycle

Program environment and pretreatment

什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。

redis-扩展数据类型(跳跃表/BitMaps/HyperLogLog/GeoSpatial)

The process of finding free screen recording software - I didn't expect win10 to come with this function

Zhiniu stock -- 09

Part 66: monocular 3D reconstruction point cloud

S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
随机推荐
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
S4/hana mm & SD EDI Nast based integrated configuration (orders, ordrsp, desadv, invoice)
utility实用组件学习之swap,move,forward,exchange
S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
Macro task, micro task and event cycle mechanism
【MUDUO】Thread封装
Leetcode 0136. numbers that appear only once: XOR
Zhiniu stock -- 09
Three board axe! Help you become an excellent software engineer
Docker 安装 Redis-5.0.12(远程访问)
Get the data of Mafeng Hotel
A long detailed explanation of C language operators
LeetCode 0136. 只出现一次的数字:异或
ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
The expression of flag=false if (flag) {return} timer=null if (timer) {return} in the throttle valve has been unclear
arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置
Program environment and pretreatment
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
Swap, move, forward, exchange of utility component learning
762. Prime number calculation setting in binary representation