当前位置:网站首页>Quick sort template
Quick sort template
2022-07-28 20:14:00 【Tiredd】
Title Description

#include <iostream>
using namespace std;
const int N = 100010;
int n, q[N];
void quick_sort(int l, int r)
{
if(l >= r) return ;
int x = q[l + r >> 1], i = l - 1, j = r + 1; // the reason being that do i ++, So stagger at the beginning 1 position , Then go in, just from l and r Start
while(i < j)
{
do i ++ ; while(q[i] < x); // Because after the exchange ,i Go ahead ,j Go back , So simply do i ++ To write
do j -- ; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
// Because the condition of out circulation is i > j, So either i == j, So divide the region l~j,j+1~r
quick_sort(l, j);
quick_sort(j + 1, r);
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(0, n - 1);
for(int i = 0; i < n; i ++ ) printf("%d ", q[i]);
}Extended topics : The first k Number
Title Description :

Code :
#include <iostream>
using namespace std;
const int N = 100010;
int n, q[N], k;
int ans;
void quick_sort(int l, int r, int k)
{
if(l >= r)
{
ans = q[l];
return ;
}
int x = q[l + r >> 1], i = l - 1, j = r + 1; // the reason being that do i ++, So stagger at the beginning 1 position , Then go in, just from l and r Start
while(i < j)
{
do i ++ ; while(q[i] < x); // Because after the exchange ,i Go ahead ,j Go back , So simply do i ++ To write
do j -- ; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
// Because the condition of out circulation is i < j, So either i == j, So divide the region l~j,j+1~r
if(j - l + 1 >= k)
quick_sort(l, j, k);
else
quick_sort(j + 1, r, k - (j - l + 1));
}
int main()
{
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(0, n - 1, k);
printf("%d", ans);
return 0;
}边栏推荐
- Why is there no log output in the telnet login interface?
- C language array and bubble sort
- 通信网络基础知识01
- Information management system and games based on C language
- Article translation software - batch free translation software supports major translation interfaces
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- 9. Pointer of C language (1) what is pointer and how to define pointer variables
- The privatized instant messaging platform protects the security of enterprise mobile business
- [C language] guessing numbers game [function]
- [NPP installation plug-in]
猜你喜欢
随机推荐
[C language] print pattern summary
WPF--实现WebSocket服务端
JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
Return and job management of saltstack
How many types of rain do you know?
[C language] scanf format input and modifier summary
软考中级(系统集成项目管理工程师)高频考点
Design of air combat game based on qtgui image interface
8. Compilation errors of C language and Chinese explanation
Andorid system layout, values, drawable adaptation
Source insight project import and use tutorial
Simple use of robobrowser
Solve flask integration_ Error reporting in restplus
What is the process of swing event processing?
Richpedia: A Large-Scale, Comprehensive Multi-Modal Knowledge Graph
C language implementation of strncpy
中国能否在元宇宙的未来发展中取得突破,占领高地?
Crawl IP
Information management system and games based on C language
跨区域网络的通信学习静态路由









