当前位置:网站首页>快速排序模板 & 注意事项
快速排序模板 & 注意事项
2022-06-22 20:03:00 【GHOSTANDBREAD】
注意事项:
如果每次取区间起点或者终点作为分界点 x,则会超时。
分界点换成随机值,或者区间中点即可。
代码:
vector数组版:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
void quick_sort(vector<int> &a, int l, int r) {
if(l >= r) return;
int x = a[l + r >> 1], i = l - 1, j = r + 1; // 这里分界点x取中间值即可,取l会超时
while(i < j) {
do i ++; while(a[i] < x);
do j --; while(a[j] > x);
if(i < j) {
a[i] ^= a[j] ^= a[i] ^= a[j];
}
}
quick_sort(a, l, j);
quick_sort(a, j + 1, r);
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i ++) {
cin >> a[i];
}
quick_sort(a, 0, n - 1);
for(int i = 0; i < n; i ++) {
cout << a[i] << " ";
}
return 0;
}
数组版:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N];
void quick_sort(int a[], int l, int r) {
if(l >= r) return;
int x = a[l + r >> 1], i = l - 1, j = r + 1;
while(i < j) {
do i ++; while(a[i] < x);
do j --; while(a[j] > x);
if(i < j) {
a[i] ^= a[j] ^= a[i] ^= a[j];
}
}
quick_sort(a, l, j);
quick_sort(a, j + 1, r);
}
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i ++) {
cin >> a[i];
}
quick_sort(a, 0, n - 1);
for(int i = 0; i < n; i ++) {
cout << a[i] << " ";
}
return 0;
}sort函数版:
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000010];
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i ++) cin >> a[i];
sort(a, a + n);
for(int i = 0; i < n; i ++) cout << a[i] << " ";
return 0;
}边栏推荐
- ACM. HJ24 合唱队 ●●
- Final review of scientific and technological literature of Shandong University (Personal Crash Course)
- 【876. 链表的中间结点】
- redis学习笔记
- 苹果GCD源代码
- ≥server2012R2系统,禁用系统自带的部分计划任务
- [the penultimate node in the linked list]
- R language Midwest dataset visualization
- 88- widely circulated parameter optimization, honey or poison?
- 【206. 反转链表】
猜你喜欢
![[876. intermediate node of linked list]](/img/c8/463d150bc6c88cfb57e94795957b0e.png)
[876. intermediate node of linked list]
![[redis]Redis6的主从复制](/img/47/3be33a0d7435bd75cdd6e7b4ea51d4.png)
[redis]Redis6的主从复制

杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
![[20. valid brackets]](/img/e9/50f327048055b07b68e694a9003130.png)
[20. valid brackets]

为了不曾忘却的纪念:孙剑专题
![[21. merge two ordered linked lists]](/img/ce/45b8cc740c8632f0cedc3ffd31620a.png)
[21. merge two ordered linked lists]

访问成功但抛出异常:Could not find acceptable representation
![[redis]集群与常见错误](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis]集群与常见错误

How swiftui simulates the animation effect of view illumination increase

【21. 合并两个有序链表】
随机推荐
513. find the value in the lower left corner of the tree / Sword finger offer II 091 Paint the house
Objective-C不同数据类型占用字节大小
Watch,computed和methods的区别
Set up your own website (12)
软考必备资料大放送,全科目软考资料都给你备好了!
The access succeeds but an exception is thrown: could not find acceptable representation
NBA playoff match chart
2022化工自动化控制仪表考试练习题及在线模拟考试
访问成功但抛出异常:Could not find acceptable representation
【160. 相交链表】
NumPy学习笔记(六)——sum()函数
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
53页智慧校园智能化系统设计方案(附下载)
MySql给某列字段添加(追加)前缀后缀
Simulated 100 questions and simulated examination of hoisting machinery command examination in 2022
SwiftUI如何模拟视图发光增大的动画效果
百家讲坛 黄帝内经(第一部)
苹果GCD源代码
[redis]redis6 master-slave replication
杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】