当前位置:网站首页>Why is quicksort so fast?
Why is quicksort so fast?
2020-11-06 21:04:00 【Yard farmland】
Quick sort
First, choose a benchmark pivot, And then go through the array ,
- The less than pivot All moved to pivot Left side ,
- The greater than pivot All moved to pivot To the right of .
thus , This pivot That's where it's located , That is to say, it's arranged 1 Elements .
Then on pivot On the left The order of the number of ,
Yes pivot On the right The order of the number of ,
It's done. .
How about the left and right ?
answer : Same method .
So the express line is also used Divide and conquer method Thought .
「 branch 」
Select a pivot, The problem is divided into
- pivot On the left
- pivot On the right
These two questions .
「 cure 」
It's the method described at the beginning , Until within each interval There is no element or only one element left You can go back to .
「 close 」
Put together, naturally it is .
But how to choose this pivot?
Take the middle one ?
Take the first one ?
Take the last one ?
for instance :{5, 2, 1, 0, 3}.
Take the last one , Namely 3.
And then we need to get rid of 3 The other number is divided into 「 Than 3 Big 」 and 「 Than 3 Small 」 The two part , This process is called partition( Divide ).
We still use 「 Baffle method 」 Thought , You don't have to actually have two arrays to store these two parts , It's using two baffles , I've divided the intervals .
We use it 「 Two pointers 」( It's the baffle ) Divide the array into 「 Three intervals 」, that
- The range on the left is used to put less than pivot The elements of ;
- The range on the right is used to put greater than pivot The elements of ;
- In the middle is the unsorted interval .
So when initializing , We need to make sure 「 Unordered range 」 Be able to contain except 3 All the elements beyond , therefore
- Unordered range = [i, j]
So the left and right sections become :
- [0, i): Compare 3 Small number ;
- (j, array.length -2]: Compare 3 Large number
Be careful ️ i, j It's not included in the left and right ranges .
So our goal is check Every number in an unsorted interval , And put it in the right range , To narrow the unsorted range , Until there are no unordered elements .
From left to right check:
Step1.
5 > 3, therefore 5 Put it in the right range , therefore 5 and j Point to the 0 In exchange :
such 5 Just line up , The pointer j --, So our unsorted interval is one less number ;
Step2.
0 < 3, So it should be on the left , direct i++;
Step3.
2 < 3, Empathy ,i++;
Step4.
1 < 3, Empathy ,i++;
So when the two hands are out of place , We end the cycle .
But it's one step short ,3 Not in the right place . So insert it between two intervals , That is, with the pointer i In exchange .
Sister Qi declared that : We are not encouraged to put pivot On the far left .
Basically all the books are on the right , Since the left and right sides are the same , We will follow the default of everyone 、 Come to a consensus , There's no need to “ new in order to be different ”.
For example, the four stars in go , But what pays attention to chess is to set your own star position first , Instead of reaching out to the opponent's side .
Then when we put pivot After changing back to the right position , Whole partition It's over .
And then it's recursive , Just sort the left and right sides .
Finally, there are two questions I would like to discuss with you :
- Back to the beginning choice pivot The problem of , Take the last one every time , How about this ?
answer : Not good .
Because we want to split the array More even , Uniform time complexity is lower ; But if it's an ordered array , So the last one is always the most uneven one .
So it should be Random take pivot, In this way, we can avoid the situation that the array itself always gets the maximum value .
- pivot Where to put it
After random selection , We still have to take this pivot To the far right of the entire array , So our unsorted interval is Successive , Otherwise, every time I walk to pivot There's also the thought of skipping it , I'm so tired .
class Solution {
public void quickSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
quickSort(array, 0, array.length - 1);
}
private void quickSort(int[] array, int left, int right) {
// base case
if (left >= right) {
return;
}
// partition
Random random = new Random(); // java.util The random number generator in
int pivotIndex = left + random.nextInt(right - left + 1);
swap(array, pivotIndex, right);
int i = left;
int j = right-1;
while (i <= j) {
if (array[i] <= array[right]) {
i++;
} else {
swap(array, i, j);
j--;
}
}
swap(array, i, right);
//「 branch 」
quickSort(array, left, i-1);
quickSort(array, i+1, right);
}
private void swap(int[] array, int x, int y) {
int tmp = array[x];
array[x] = array[y];
array[y] = tmp;
}
}
The space-time complexity here has a lot to do with whether the points are uniform or not , So let's look at the situation :
1. Divide equally
Time complexity
If you can divide it almost evenly every time , that
- This is the main time of each cycle while In circulation , That is to say O(right - left);
- If it's even, it's logn layer ;
- So the total time is O(nlogn).
Spatial complexity
- The height of the recursive tree is logn,
- The space complexity of each floor is O(1),
- So the total spatial complexity is O(logn).
2. Most uneven
If you can get the maximum every time / minimum value , So the recursive tree looks like this :
Time complexity
As shown in the figure above :O(n^2)
Spatial complexity
The height of this recursive tree becomes O(n).
3. summary
Actually , Most of the time it will be close to uniform The situation of , So the uniform case is a average case.
Why does it seem like the best is actually an average situation ?
Because even if you don't get to the middle point , For example, it is divided into 10% and 90% The numbers on both sides , In fact, the time of each floor is still O(n), It's just The number of layers has changed to 9 At the bottom of the log, The total time is still O(nlogn).
So the average time complexity of the fast queue is O(nlogn).
stability
Then you should be able to see it , stay swap When , Has broken the relative order between the elements , So fast platoon is not stable .
We also answer the question at the beginning , Namely
-
Why for primitive type Use fast platoon ,
- Because it's the fastest ;
-
Why for object Use merge ,
- Because it's stable and fast .
That's all the content of the quick queue , It's also a very common test ! In the next article, I'll talk about a few topics extended from quick scheduling , Guess what it is ?
If you like this article , Remember to leave me a like message ~ Your support and recognition , It's the biggest driving force of my creation , See you in the next article !
This is Qi , New York program , Lifelong learners , Every night 9 spot , Let's meet you in the cloud study room !
See my... For more dry articles Github: https://github.com/xiaoqi6666/NYCSDE
版权声明
本文为[Yard farmland]所创,转载请带上原文链接,感谢
边栏推荐
- The method of realizing high SLO on large scale kubernetes cluster
- 事务的隔离级别与所带来的问题
- EOS founder BM: what's the difference between UE, UBI and URI?
- 2020年数据库技术大会助力技术提升
- Vue communication and cross component listening state Vue communication
- image operating system windows cannot be used on this platform
- 【转发】查看lua中userdata的方法
- 代码重构之法——方法重构分析
- 【自学unity2d传奇游戏开发】地图编辑器
- To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
猜你喜欢
Vue communication and cross component listening state Vue communication
Introduction to the structure of PDF417 bar code system
CCR coin frying robot: the boss of bitcoin digital currency, what you have to know
What are the common problems of DTU connection
An article will take you to understand CSS3 fillet knowledge
Ronglian completed US $125 million f round financing
行为型模式之备忘录模式
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
ES6 learning notes (5): easy to understand ES6's built-in extension objects
Using an example to understand the underlying processing mechanism of JS function
随机推荐
Multi robot market share solution
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
What are manufacturing and new automation technologies?
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
Diamond standard
jenkins安装部署过程简记
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
统计项目代码行数
How does filecoin's economic model and future value support the price of fil currency breaking through thousands
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
C#和C/C++混合编程系列5-内存管理之GC协同
C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
小游戏云开发入门
GUI engine evaluation index
mongo 用户权限 登录指令
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
Small program introduction to proficient (2): understand the four important files of small program development
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
Swagger 3.0 brushes the screen every day. Does it really smell good?