当前位置:网站首页>快速排序简单思路及程序
快速排序简单思路及程序
2022-06-21 16:02:00 【And ν】
输入n个数,将其进行又小到大的排序
首先输入n个数,并将其保存到数组a[n]中。
例如我有以下一组数:
| 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|
| 34 | 57 | 78 | 9 | 54 |
首先我们要理解什么是快速排序?
它是由冒泡排序演变而来,人们在进行大规模排序运算时便发现使用冒泡法排序所消耗的时间太长,运算次数达到了O(n^2)。于是就有了快速排序的诞生,其运算级也缩减到O(n log n)。
冒泡排序是从左到右与相邻的数比较互换,多次遍历取得结果,而快速排序则是先找到一个基点,利用程序,将数组中小于它的值都排到它的左边,大于它的数都排到它的右边,利用递归思想,以它为分界点将其分为两个数组,重复操作,直至其左右两边都只有一个数值时,停止,这是从左到右的快速排序就完成了。
先定义一个变量t,用来存放基点值。再定义两个变量用来遍历:
| 34 | 57 | 78 | 9 | 54 |
|---|---|---|---|---|
| i-> | <-j |
从j开始依次遍历下去,直至其所对应的值小于基点值,并将a[i]=a[j],如图所示:
| 9 | 57 | 78 | 9 | 54 |
|---|---|---|---|---|
| i-> | j |
又从i开始遍历,直至找到其所对应的值大于基点值,又将a[j]=a[i],
如图所示:
| 9 | 57 | 78 | 57 | 54 |
|---|---|---|---|---|
| i | <-j |
用循环重复此操作,直至i=j时停止。
| 9 | 57 | 78 | 57 | 54 |
|---|---|---|---|---|
| i、j |
此时可以确保其i左边的值均小于基点值,其j右边的值均大于基点值,因此此时i与j所在的位置便是基点值在这组数组中所占据的位置,再将a[i]=t。
在以该基点值为界,对其左右两边的值分别进行此操作,直至其基点值两边只剩下一个数值。(用递归实现)
代码如下:
#include<iostream>
using namespace std;
void quicksort(int *a,int b,int c)
{
int n = a[b], i = b, j = c;
if (b >= c)
return;
while (i < j)
{
while (a[j] > n && j > i)
j--;
a[i] = a[j];
while (a[i]<n && j>i)
i++;
a[j] = a[i];
}
a[i] = n;
quicksort(a, b, i - 1), quicksort(a, j + 1, c);
}
int main()
{
int a[100] = { 0 }, n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
quicksort(a, 0, n - 1);
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
return 0;
}边栏推荐
- In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
- Cisco(59)——Hub&Spoke MPLS
- 数据分析必备:6大步骤+5大类型+2大分析方法
- Disruptor本地线程队列_WorkProcessor异常_FatalExceptionHandler---线程间通信工作笔记004
- ESP8266/ESP32 通過TimeLib庫獲取NTP時間方法
- tinymce.init()浏览器兼容问题
- Pytest framework implements pre post processing
- Unittest框架的测试日志
- 微信小程序开发入门教程-文本组件介绍
- 集成底座方案演示说明
猜你喜欢

撰写有效帮助文档的7大秘诀

Huawei (13) - route introduction

Yaml data driven demo

学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条

云原生之混合云网络互联
![[observation] Microsoft's](/img/70/c598ef50a4c06f9bdcb7a935dbf62b.png)
[observation] Microsoft's "cloud + end" comprehensive innovation makes hybrid cloud simpler, more flexible and more secure
![Undefined functions or variables [explained in one article] (matlab)](/img/fe/54272b8efce87ed7a78ac43b1fc189.png)
Undefined functions or variables [explained in one article] (matlab)

Generating test reports using the unittest framework

Yaml数据驱动演示

智能制造的下一站:云原生+边缘计算双轮驱动
随机推荐
Cloud native monitoring system - Nightingale's recent list of new functions to solve multiple production pain points
Circular of the State Council on regulating the management of the rental of housing with common property rights (for Trial Implementation)
Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers
数据分析必备:6大步骤+5大类型+2大分析方法
Cisco(35)——BGP入门实验
qtcreator報錯解决
I do 3D restoration for the aircraft carrier: these three details are shocking
[SQLite] résoudre le jeton non enregistré: ''
Esp8266 / esp32 obtenir la méthode NTP time via la Bibliothèque timelib
Focusing on industrial intelligence scenarios, Huawei cloud recruits partners to help solve transformation problems
【1108. IP 地址无效化】
Kindeditor uploading pictures and using
为什么要做茶叶商城小程序app开发?
一体化伺服电机与施耐德PLC TM241CEC24T在Canopen协议下的应用
Show you how to distinguish several kinds of parallelism
Do Internet companies do unit tests? Is it necessary to do unit testing for the needs of the bank?
Yaml file details
学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条
Fisher信息量检测对抗样本代码详解
Résolution des erreurs signalées par qtcreator