当前位置:网站首页>[1. quick sort]
[1. quick sort]
2022-06-22 02:41:00 【Little silly bird_ coding】
Quick sort
Ideas :
- Determine the cut-off point :q[l] , q[(1 + r)/2] , q[r] , Random
- Adjust the range of the interval : Make in
On the left side of the dividing point is the number all <= x,The numbers on the right of the dividing point are all >= x(Key treatment)- Recursively handle the left and right segments
Method 1: The realization idea of violence method
- Create two arrays
a[ ] , b[ ]- Traverse
q[l ~ r], If the currentq[i] <= x, Just insert into the array a[ ] in , If the currentq[i] >= x, Just insert into the array b[] in .- take a[ ] Put the number in q[ ] in , And then b[ ] Put the number in q[ ] in , here q[ ] The numbers are ordered .
characteristic :
- It takes two extra spaces a[ ] , b[ ]
- The time complexity is still O(n)
Method 2: Realization of double pointer
- Two hands
i , jOne pointing to the left , The other one points to the right , Two handsAt the same time, go to the middle- as long as
i < x, here i Go toRightMove a bit , Until I meti >= x, herei Stop moving, Start moving j , as long asj > x, j Just goLeftMove a bit , untilj <= x, here j Stop moving .In exchange fori and j After the value of , Pointer all the wayMove one bit in the middle.- until i and j
Meet or pass through. here i The numbers on the left are <= x , j The numbers on the right are >= x.- Compare the number on the left with the number on the right , Continue recursion separately , Repeat the above steps .
give an example
subject
Give you a length of n The whole number sequence of .
Please use quick sort to sort this sequence from small to large .
And output the ordered sequence in order .
Input format
There are two lines of input , The first line contains integers n.
The second line contains n It's an integer ( All integers are in 1∼109 Within the scope of ), Represents the whole sequence of numbers .
Output format
The output is one line , contain n It's an integer , It's an ordered sequence .
Data range
1 ≤ n ≤ 100000
sample input :5 3 1 2 4 5sample output :
1 2 3 4 5
Code
#include <iostream> using namespace std; const int N = 1e6 + 10; int n; int q[N]; void quick_sort(int q[], int l, int r) { if (l >= r) return; int x = q[(l + r) / 2], i = l -1 ,j = r + 1; // Set the dividing point while (i < j) // Loop traversal , When i And j If it overlaps or crosses, it exits { do i ++; while (q[i] < x); do j --; while (q[j] > x); if (i < j) swap(q[i], q[j]); } quick_sort(q, l ,j); // Recursive two sides respectively quick_sort(q, j + 1, r); } int main() { int i; scanf("%d", &n); for (i = 0; i < n; i ++) scanf("%d", &q[i]); quick_sort(q, 0, n - 1); for (i = 0; i < n; i ++) printf("%d ", q[i]); return 0; }In addition to using do while, You can also use while loop
void quick_sort(int array[], int low, int high) { int i = low; int j = high; if(i >= j) { return; } int temp = array[low]; while(i <= j) { while(array[j] >= temp && i < j) { j--; } while(array[i] <= temp && i < j) { i++; } if(i < j) { swap(array[i], array[j]); } } // Benchmark temp Put it in your place ,( The first i A place ) swap(array[low], array[i]); quick_sort(array, low, i - 1); quick_sort(array, i + 1, high); }
边栏推荐
- Brief introduction to common pigtails of communication pigtails
- 【8、一维前缀和】
- Using OKR for HR digital transformation
- Is the link of Hengtai securities VIP low commission account opening safe?
- 【Docekr学习遇到的坑】
- Wechat applet film and television comment exchange platform system graduation design (3) background function
- LabVIEW开发发电厂合规性测试系统
- Show you how to distinguish several kinds of parallelism
- Informer有什么
- 使用 Neo4j 沙箱学习 Neo4j 图数据科学 GDS
猜你喜欢

EMC Radiation Emission rectification - principle Case Analysis

最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA

Graphacademy course explanation: introduction to neo4j graph data science

Wechat applet film and television review and exchange platform system graduation design (1) development outline

基于xposed框架hook使用
![[Chapter 26 medical impact segmentation system based on minimum error method and region growth -- matlab deep learning practical GUI project]](/img/19/18dcc8ed017541d91c52550f48923c.png)
[Chapter 26 medical impact segmentation system based on minimum error method and region growth -- matlab deep learning practical GUI project]

rt_thread的消息队列

国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了

Brief introduction to common pigtails of communication pigtails

Chapter 24 image and video processing based on Simulink -- matlab in-depth learning and practical collation
随机推荐
理想L9正式发布:8月底前开始交付 零售价45.98万元
MATLAB 学习笔记(5)MATLAB 数据的导入和导出
Ioerror: no translation files found for default language zh cn Solutions for
June25,2022 PMP Exam clearance manual-3
【9. 子矩阵和】
What is a neural network
GraphConnect 2022 大会的产品发布一览
Relative references must start with either “/“, “./“, or “../“.
如何选择合适的 Neo4j 版本(2022版)
Implementation differences between import and require in browser and node environments
EMC rectification tips
What is a neural network
Asemi fast recovery diode FR107 parameter, FR107 real object, FR107 application
自动化工具-监测文件的变化
PMP项目管理知识该如何运用?
Zap grammar sugar
Using OKR for HR digital transformation
Chapter 24 image and video processing based on Simulink -- matlab in-depth learning and practical collation
关于PMP考试,你想知道的知识都在这里了
Write your own kubernetes controller


