当前位置:网站首页>5、快速(分组)排序
5、快速(分组)排序
2022-06-09 04:08:00 【允谦呀】
5、快速(分组)排序
快速排序的流程如下:
- 首先设定一个分界值,通过该分界值将数组分成左右两部分。
- 将大于等于分界值的数据集中到数组的右边,小于分界值的数据集中到数组的左边。此时,左边部分中各元素都小于等于分界值,而右边部分中各元素都大于等于分界值。
- 然后左边和右边的数据可以独立排序,对于左侧的数据,又可以取一个分界值,将该部分数据分成左右两部分,同样将左边放置较小值,右边放置较大值。右侧的数组也可以做类似的处理
- 重复上述过程,可以看除,这是一个递归定义通过递归,将左侧部分排好序后,在递归排好右侧部分的顺序。当左右两部分各数据排序完成后,整个数组的排序也就完成了。
#include <bits/stdc++.h>
using namespace std;
void quick_sort(int nums[], int low, int high){
//出口条件
if(low >= high){
return;
}
int l = low;
int r = high;
int tmp = nums[l];
while(l < r){
//以tmp为界限,比tmp大的放左边
while(nums[r] > tmp && l < r){//理解思路:如果右边的指针指向的数比tmp大,也就不是我们要找的数 ,所以指针要前移
r--;
}
//以tmp为界,比tmp小于等于的放右边
while(nums[l] <= tmp && l < r){//理解思路:如果右边的指针指向的数比tmp小,也就不是我们要找的数 ,所以指针要后移
l++;
}
if(l < r){//左右指针都找到符合条件的数之后呢,并且在l < r(左右指针没有相遇)的前提下,就交换一下左右指针所指的数
swap(nums[l],nums[r]);
}
}
swap(nums[low],nums[r]);
//递归左序列
quick_sort(nums,low,l-1);
//递归右序列
quick_sort(nums,l+1,high);
}
int main(){
int nums[10] = {50,100,40,80,30,20,60,90,70,5};
quick_sort(nums,0,9);
for(int i = 0; i < 10; i++){
cout << nums[i] << " ";
}
return 0;
}
边栏推荐
- 软件测试(二)
- 17billion parameters, 28 open test sets SOTA, the industry's largest unified visual multi task model
- 域名解析(公网)
- Merkle Patricia trie principle
- 渲染管线----通俗易懂向面试官介绍
- Getting started with Maui custom drawing
- Status mode simulates elevator operation
- 几何运用题
- Harbor容器安装以及相关特性部署与使用(SSL证书+AD域)
- VGA display of color bar, character and picture based on FPGA
猜你喜欢

OpenInfra基金会发起“定向基金”计划,推行成功开源治理经验

Pdf splitting based on pyqt5
![[share] network packet loss fault handling scheme](/img/40/39e6548651eaeec5e6c1c39651cc88.png)
[share] network packet loss fault handling scheme

渲染管线----通俗易懂向面试官介绍

JVM memory view and setting ideas

Memory surge problem location

Golang - - paquet d'exécution simultané

状态模式模拟电梯运行

强烈推荐这款神器,一行命令将网页转PDF!
![[leetcode] day 48 - 1037 Effective boomerang](/img/7f/9d353641ce62aaf52e26c07361e0cb.png)
[leetcode] day 48 - 1037 Effective boomerang
随机推荐
Face detection, training and recognition based on OpenCV
Dart: Foundation
Affichage de barres de couleur, de caractères et d'images VGA basé sur FPGA
基于PyQt5完成的pdf转word
《Attention-ocr-Chinese-Version-mas # ter》代码运行逻辑
Detailed explanation of HLS live broadcast protocol m3u8
Binary processing of opcv image
CPU surge problem location
Differences and principles between hashrouter and historyrouter
This artifact is highly recommended. One line command will convert the web page to PDF!
变量提升和函数提升
汇编:CPU结构 - FLAG标志寄存器和相关指令
[share] network packet loss fault handling scheme
人才缺口50万以上,平均薪资20K?网络安全,测试员的下一个风口~
Kotlin basics from getting started to advanced series (Introduction) basic use of file storage
扩展芯片,hisi3559av100 i2c调试
From just entering the testing industry to doubling my salary: talk about my advanced testing experience, which is worth learning from
声学工程师应知道的150个声学基础知识(全篇)
几何运用题
目标检测模型mAP计算方法与对比步骤——对比原模型与量化模型之间的mAP