当前位置:网站首页>冒泡排序/堆排序
冒泡排序/堆排序
2022-07-20 05:29:00 【张淑芬~】
冒泡排序:每一趟循环两两比较,大的向后挪动,最终最大值放在最后,n个数据需要跑n-1趟。
代码如下:
//冒泡排序 时间复杂度O(n^2) 空间复杂度O(1) 稳定性:稳定
void BubbleSort(int* arr, int len)
{
assert(arr != NULL);
for (int i = 0; i < len-1; ++i)
{
for (int j = 0; j < len - 1 - i; ++j)
{
if (arr[j+1] < arr[j])
{
int tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
}
}
}
}
我想大多数人刚开始写的代码是上边的类型,但之后有人对冒泡进行优化,但优化的可以说是优化很小
//冒泡排序 时间复杂度O(n^2) 空间复杂度O(1) 稳定性:稳定
void BubbleSort(int* arr, int len)
{
assert(arr != NULL);
bool tag = true;//标记 首先赋初值为真
//从头到尾遍历一般,没有交换操作,则可以认定完全有序
//反过来说:只要有一次交换操作,则不能认定完全有序
for (int i = 0; i < len-1; ++i)
{
tag = true;
for (int j = 0; j < len - 1 - i; ++j)
{
if (arr[j+1] < arr[j])
{
int tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
tag = false;
}
}
if (tag)
{
break;
}
}
}
就是加了一个bool类型,只要头到尾遍历一般,没有交换操作,则可以认定完全有序,直接退出。
堆排序:
先搞清楚什么是大顶堆,小顶堆
大顶堆:父节点的值大于孩子节点(从小到大排序用大顶堆)
小顶堆:父节点的值小于孩子节点(从大到小排序用小顶堆)
1。我们需要先将原始数据想象成一个完全二叉树
2.从最后一个非叶子节点开始调整,调整为大顶堆
此时我们会发现根节点的值是数组中最大的那个值
3.将根节点和最后一个节点的值进行交换,然后将这个尾结点踢除我们的排序数组即可
4.重复2,3步骤
代码如下:
//一次调整函数 时间复杂度O(logn)
void HeapAdjust(int arr[], int start, int end)
{
//assert
int tmp = arr[start];
for(int i=start*2+1; i<=end; i=start*2+1)//start*2+1 相当于是start这个节点的左孩子
{ //i<end 退出for循环 触发的是情况1
if(i<end && arr[i+1] > arr[i])//i<end 代表存在右孩子,且右孩子的值还大于左孩子
{
i++;//则此时,让i指向右孩子
}
//此时i肯定已经指向较大的那个孩子
if(arr[i] > tmp)//子大于父
{
arr[start] = arr[i];
start = i;
}
else
{
break;//退出for循环,触发情况2
}
}
arr[start] = tmp;
}
//堆排序:时间复杂度O(n*logn) 空间复杂度O(1) 稳定性:不稳定
void HeapSort(int *arr, int len)
{
//1.整体从最后一个非叶子节点开始由内到外调整一次
//首先需要知道最后一个非叶子节点的下标
for(int i=(len-1-1)/2; i>=0; i--)//因为最后一个非叶子节点肯定是 最后一个叶子节点的父节点
{
HeapAdjust(arr, i, len-1);//调用我们一次调整函数 //这里第三个值比较特殊,没有规律可言,则直接给最大值len-1
}
//此时,已经调整为大顶堆了
//接下来,根节点的值和当前最后一个节点的值进行交换,然后将尾结点剔除掉
for(int i=0; i<len-1; i++)
{
int tmp = arr[0];
arr[0] = arr[len-1-i];//len-1-i 是我们当前的尾结点下标
arr[len-1-i] = tmp;
HeapAdjust(arr, 0, (len-1-i)-1);//len-1-i 是我们当前的尾结点下标,然后再给其-1则相当于将其剔除出我们的循环
}
}
边栏推荐
猜你喜欢
[scientific literature measurement] analysis and visualization of readability indicators of Chinese and English literature titles and abstracts
Matlab digital image processing experiment 5: morphological image processing
Millet college reported an error 503 when registering the gateway gateway with Nacos
DOM -- event syntax
LabVIEW中的自动保存功能
Apache Doris Binlog Load使用方法及示例
CTFshow-web入门信息收集-wp(1-20) (详解)
Binary tree implementation (generate binary tree according to hierarchical array)
【笔记】Logstash环境搭建和安装配置
30 spark introduction: Spark Technology stack explanation, partition, system architecture, operator and task submission method
随机推荐
Matplotlib教程(一)【初识Matplotlib】
2022-7-19 Gu Yujia's learning notes of group 8 (this keyword and packaging)
基于SSM实现水果蔬菜商城管理系统
2022-07-19-利用shell去激活conda环境
Usage and examples of Apache Doris binlog load
Mysql, I'll create 200W pieces of data casually and tell you about paging optimization.
Usage and configuration of Apache Doris ODBC MySQL appearance under Ubuntu
LabVIEW中的自动保存功能
【学习笔记之菜Dog学C】练习
试题 D: 修剪灌木
Apache Doris ODBC appearance database mainstream version and its ODBC version correspondence
【学习笔记之菜Dog学C】结构体初阶
Apache Doris uses the Prometheus alertmanager module to send exception information to the nail alarm group
DNP3 simulator tutorial
Ctfshow web entry information collection WP (1-20) (detailed explanation)
2022-7-19 第八小组 顾宇佳 学习笔记 (this关键字和封装)
对ReadFile堵塞进行异步处理
Leetcode 200 Number of islands (July 19, 2022)
qt里调用win32函数
毕业季--各应用场景案例使用技术