当前位置:网站首页>61.【快速排序法详解】
61.【快速排序法详解】
2022-08-09 22:18:00 【吉士先生.】
快速排列
(一)、快速排列的基本原理:
关于快速排序,它的基本思想就是选取一个基准,一趟排序确定两个区间,一个区间全部比基准值小,另一个区间全部比基准值大,接着再选取一个基准值进行排序,依次类推,最后得到一个有序的数列.
(二)、运用快速排列的注意事项:
切记在while 循环中,时是右边的先动,然后左边的后动,前后位置可不能整反.
(三)、基本思想和思路:
首先我们要进行函数调用设置:把数组的地址,起始位置、终点位置都要传送给回调函数,然后我们要在回调函数中进行一下操作,判断左边位置是否要小于右边位置,假如说小于进行: while循环,进行i++,j–操作,当就遇到的数字比基准值小的时候就停止,i遇到比基准值大的数值时停止,假如说此时此刻的位置是i<j的,那么就让他俩交换数据.如果时i>j的那么就要i的值和基准值进行交换。退出while循环,然后再重新定义一个基准值进行交换,然后再递归两次函数即可.
(四)、实战项目:
1.升序代码展现:
#include <iostream>
#include <string.h>
using namespace std;
void Qicklysort(int arry[],int left, int right)
{
if (left >= right)return; //假如说左边大于等于右边 退出这个结构体
int l = left;
int r = right;
int base = arry[left];
while (l < r)
{
while (l < r && arry[r] >= base) //从右向左开始移动直到遇到小于base的值为止
{
r--;
}
while (l < r && arry[l] <= base) //从左向右开始移动直到遇到大于base的值为止。
{
l++;
}
if (l < r) //停止后是为了交换位置:
{
int temp;
temp = arry[l];
arry[l] = arry[r];
arry[r] = temp;
}
}
base = arry[left];
arry[left] = arry[l]; //目的是为了让基准数和最左边的数据交换换
arry[l] = base;
Qicklysort(arry, left, l - 1); //判断从0到l的项目 ,不包括l
Qicklysort(arry, l+1, right);
}
int main()
{
int n, a[100];
cout << "请输入你要排序的个数:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "请输入第" << i + 1 << "个数:" << endl;
cin >> a[i];
}
Qicklysort(a, 0, n - 1);
cout << "排序后为:" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}
2.升序效果展现:
3.降序代码展现:
#include <iostream>
#include <string.h>
using namespace std;
void Qicklysort(int arry[],int left, int right)
{
if (left >= right)return;
int l = left;
int r = right;
int base = arry[left];
while (l < r)
{
while (l < r && arry[r] <= base) //改变
{
r--;
}
while (l < r && arry[l] >= base) //改变
{
l++;
}
if (l < r)
{
int temp;
temp = arry[l];
arry[l] = arry[r];
arry[r] = temp;
}
}
base = arry[left];
arry[left] = arry[l];
arry[l] = base;
Qicklysort(arry, left, l - 1);
Qicklysort(arry, l+1, right);
}
int main()
{
int n, a[100];
cout << "请输入你要排序的个数:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "请输入第" << i + 1 << "个数:" << endl;
cin >> a[i];
}
Qicklysort(a, 0, n - 1);
cout << "排序后为:" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}
4.降序效果展现:
(五)、if(表达式)return; 介绍
1.简单介绍:
if()return;表示假如表达式为真,那么后面的就不再执行了。为假就继续执行.
2.代码展示:
切记主函数可能为 int 只能为void
#include <iostream>
using namespace std;
void main()
{
int a = 3;
cout << "a1=" << a << endl;
if (a < 4) return;
cout << "a2=" << a << endl;
cout << "a3=" << a << endl;
}
3.效果展示:
(六)、EOF介绍:
1.简单说明:
2.代码展示:
3.效果展示:
边栏推荐
猜你喜欢
2022年最新《谷粒学院开发教程》:10 - 前台支付模块
iNFTnews | 迪士尼如何布局Web3
daemon
Mysql/stonedb - slow SQL - 2022-08-09 Q16 analysis
matplotlib散点图颜色分组图例
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
都在说云原生,那云原生到底是什么?
The 2022-8-9 sixth group of input and output streams
直播预告 | ICML 2022 11位一作学者在线分享神经网络,图学习等前沿研究
Has your phone ever been monitored?
随机推荐
高数_复习_第4章:向量代数和空间解析几何
Filament - Material basic graphics drawing
【哲理】事教人
联盟链技术应用的难点
Day 12 of learning to program
杂谈——程序员的悲哀
Gumbel distribution of discrete choice model
友元类和友元函数
Janus Official DEMO Introduction
tiup cluster scale-out
干货!迈向鲁棒的测试时间适应
一体化伺服电机在三轴钻孔机中的应用
leetcode 20. Valid Parentheses 有效的括号(中等)
打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
JS--popstate事件--使用/教程/实例
tiup cluster stop
kubesphere
【JZOF】32从上往下打印二叉树
[Cloud Native] This article explains how to add Tencent Crane to Kubevela addon
Chapter 15 HMM模型