当前位置:网站首页>快速排序模板 & 注意事项
快速排序模板 & 注意事项
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;
}边栏推荐
- PHP image making
- ≥server2012R2系统,禁用系统自带的部分计划任务
- 【ICML2022】利用虚拟节点促进图结构学习
- Evaluation index and code realization (ndcg)
- 长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!
- 杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
- Use Charles to capture packets
- R language airpassengers dataset visualization
- Operation of simulation test platform for 2022 Shandong safety officer C certificate test
- 80- paging query, not only writing
猜你喜欢

R language airpassengers dataset visualization

53 page intelligent campus intelligent system design scheme (download attached)
![[redis]配置文件](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis]配置文件
![[book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!](/img/f0/4f237e7ab1bff9761b6092dd4ef3d9.png)
[book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!

基于C语言开发工资管理系统 课程论文+源码及可执行exe文件

建立自己的网站(12)

【OR36 链表的回文结构】
![[21. merge two ordered linked lists]](/img/ce/45b8cc740c8632f0cedc3ffd31620a.png)
[21. merge two ordered linked lists]

万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问

NFT,只可远观不可亵玩焉
随机推荐
php 镜像制作
NFT,只可远观不可亵玩焉
[redis]redis6 master-slave replication
RealNetworks vs. 微软:早期流媒体行业之争
[book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!
ACM. HJ45 名字的漂亮度 ●●
Baijia forum Huangdi Neijing (Part I)
【剑指Offer】面试题44.数字序列中的某一位数字
2022年A特种设备相关管理(电梯)考题及模拟考试
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha
Agricultural futures account opening
Apple Objective-C source code
Final review of scientific and technological literature of Shandong University (Personal Crash Course)
513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
大势智慧创建倾斜模型和切割单体化
The access succeeds but an exception is thrown: could not find acceptable representation
杰理之在music模式下提示音使用打断模式无法播放的问题【篇】
为了不曾忘却的纪念:孙剑专题
Baijia forum Wu Zetian
73- find the SQL example during the business peak period (report development class)