当前位置:网站首页>交换排序(冒泡排序、快速排序)
交换排序(冒泡排序、快速排序)
2022-08-07 08:13:00 【HwWwWwK】
交换排序
1. 冒泡排序
- 时间复杂度O(n2)
- 空间复杂度O(1)
- 稳定
- 适用:顺序存储和链式存储的线性表
n个元素,n-1趟排序,每趟把最大的排到最后。
int i, j;
int flag;
for (i = 0; i < n - 1; i++) {
flag = 0
for (j = 0; j < n - 1 - i; j++) {
if (L[j] > L[j + 1]) {
int tmp = L[j];
L[j] = L[j + 1];
L[j + 1] = tmp;
flag = 1;
}
}
if (!flag) break;
}
2. 快速排序
- 时间复杂度O(nlog2n),最坏会退化为O(n2)
- 空间复杂度最好O(log2n),最坏O(n)
- 不稳定
- 适用:顺序存储的线性表
基于分治,每次排序将确定一个枢轴(基准)pivot
然后将表通过枢轴一分为二,保证左边均小于pivot,右边均大于pivot。
此举将保证pivot已经落在了最终序列的相应位置。
运用递归的思想,将子表重新快速排序。
void quick_sort(ElemType L[], int l, int r) {
if (l < r) {
int cut = partition(L, l, r);
quick_sort(L, l, cut - 1);
quick_sort(L, cut + 1, r);
}
}
快速排序好坏的关键取决于划分算法partition,写法根据枢轴的选择而不同
int partition(ElemType L[], int l, int r) {
ElemType cut = L[l]; // 选取最左边元素为枢轴(可不同)
while (l < r) {
while (l < r && L[r] >= cut) --r;
L[l] = L[r];
while (l < r && L[l] <= cut) ++l;
L[r] = L[l];
}
L[l] = cut;
return l;
}
3. 例
// 交换排序
int* sortArray(int* nums, int numsSize, int* returnSize)
{
*returnSize = numsSize;
int i, j;
int flag;
for (i = 0; i < numsSize - 1; i++) {
flag = 0;
for (j = 0; j < numsSize - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
flag = 1;
}
}
if (!flag) return nums;
}
return nums;
}
// 快速排序
int partition(int* L, int l, int r)
{
int cut = L[l]; // 选取最左边元素为枢轴(可不同)
while (l < r) {
while (l < r && L[r] >= cut) --r;
L[l] = L[r];
while (l < r && L[l] <= cut) ++l;
L[r] = L[l];
}
L[l] = cut;
return l;
}
void quick_sort(int* L, int l, int r)
{
if (l < r) {
int cut = partition(L, l, r);
quick_sort(L, l, cut - 1);
quick_sort(L, cut + 1, r);
}
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
*returnSize = numsSize;
quick_sort(nums, 0, numsSize - 1);
return nums;
}
边栏推荐
- Model fine-tuning transfer learning Finetune method Daquan
- Exploration and practice of Redis fixed-length queue
- DeFi 前景展望:概览主流 DeFi 协议 Q2 进展
- VoLTE Basic Self-Learning Series | What is Forking in SIP and IMS
- canvas图像绘制(有放大缩小和拖动功能)
- 强大的数
- 微服务系列一:微服务的优势与劣势
- DeFi Prospects: An Overview of Q2 Progress of Mainstream DeFi Protocols
- rest client: a lightweight vscode plugin for api requests
- org.apache.ibatis.binding.BindingException
猜你喜欢

网络互连模型、物理层、数据链路层

【Tic Tac Toe Chess】

家居江湖掀起「夺魁战」,红星美凯龙如何打造品牌增量场?

30.01 C/S and TCP/IP protocols are interesting and vivid

Are there MCUs with 5nm process technology?

Vitalik explains 5 types of ZK-EVM

30.01 C/S、TCP/IP协议妙趣横生、惟妙惟肖谈

GBL210-ASEMI机箱电源适配器整流桥GBL210

The third bullet of FPGA development: button control LED experiment

5 Crypto Themes Poised to Lead the Next Bull Market
随机推荐
C 学生管理系统_读取文件中得学生信息
力扣:494. 目标和
QT不同子类间共享变量,教你简单、规范的方法
网络安全笔记2——单钥密码体制
神经网络和pid有什么区别,基于神经网络的pid控制
Knapsack Theory 01 Knapsack (Rolling Array)
【n-piece chess】
VoLTE basic self-study series | VoWIFI architecture diagram
DCDC电源方案设计踩坑
U-Net网络
Lexicon: 416. Divide Equals and Subsets
随笔-那些快乐的日子
rest client: a lightweight vscode plugin for api requests
接口流量突增,如何做好性能调优?
【LeetCode】Day110- Divide the letter interval
LeetCode刷题笔记:206.反转链表(递归方法解决)
Redis 定长队列的探索和实践
接口的安全设计要素:ticket,签名,时间戳
Vitalik详解5种类型的ZK-EVM
【Promise】Promise 使用 / 回调地狱问题 async-await /宏队列与微队列