wordpress 嵌入地图/山东网络推广优化排名
时间复杂度
O ( 1 ) O(1) O(1)
void func1(int n){int count = 100;count++;
}
void func2(int n){int count = 100;for(int i = 0; i < count;i++){}
}
int func3(int n){return n;
}
O ( n ) O(n) O(n)
void func1(int n){int count = 100;for(int i = 0; i < n;i++){count++;}
}
O ( n 2 ) O(n^2) O(n2)
void func1(int n){int count = 100;for(int i = 0; i < n;i++){for(int j = 0;j < n;j++){count++;}}
}
O ( n m ) O(nm) O(nm)
void func1(int n,int m){int count = 100;for(int i = 0; i < n;i++){for(int j = 0;j < m;j++){count++;}}
}
O ( log n ) O(\log n) O(logn)
void func1(int n){int i = 1;while(i < n){i *= 2;}
}
O ( n log n ) O(n \log n) O(nlogn)
void func1(int n){int i = 1;for(j = 0; j < n; j++){while(i < n){i *= 2;} }
}
空间复杂度
O ( 1 ) O(1) O(1)
void func1(int n){int a = 1;a++;int b = 2;b++;
}
O ( n ) O(n) O(n)
void func1(int n){int[] array = new int[n];for(int i = 0;i < n; i++){array[i] = i;}
}int sum(int n){int a;if(n == 0) return 0;return n + sum(n - 1);
}
O ( n 2 ) O(n^2) O(n2)
void func1(int n){int[,] array = new int[n,n];
}int sum(int n){int[] array = new int[n];if(n == 0) return 0;return n + sum(n - 1);
}
稳定性和冒泡排序
稳定性:当需要排序的序列中存在相同关键字的元素,排序后的相对次序保持不变,则算法稳定,稳定的排序算法会保留数值项顺序的表达意义。
冒泡排序:
int [] array = new int[]{2,5,6,7,1,3}
void bubbleSort(int[] arr, int n){int temp;int i,j;for(i = 0;i< n - 1; i++){for(j = 0; j < n-1-i; j++){if(arr[j] > arr[j + 1]){temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}
}
时间复杂度为 O ( n 2 ) O(n^2) O(n2),空间复杂度 O ( 1 ) O(1) O(1)
改进版:
int [] array = new int[]{2,5,6,7,1,3}
void bubbleSort(int[] arr, int n){int temp;int i,j;bool flag = false; for(i = 0;i< n - 1; i++){for(j = 0; j < n-1-i; j++){flag = false;if(arr[j] > arr[j + 1]){temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;flag = true;}}if(!flag){break;}}
}
选择排序
void arr_sort(int *p,int n)
{int i,j;int min = 0;for(i = 0;i < n - 1;i++)//排序次数{min = i;for(j = i + 1;j < n;j++){if(p[j] < p[min]){min = j;//记录交换的元素下标值}}if(i != min){int temp = p[i];p[i] = p[min];p[min] = temp;} }
}
插入排序
int [] array = new int[]{2,5,6,7,1,3}
void printArray(int[] arr){ls = "";foreach(var r in arr){ls += r + ",";}Debug.Log("轮次" + index++ + "数组" + ls);
}
void insertSort(int[] arr, int n){for(i = 0;i< n; i++){for(j = i; j > 0 && arr[j-1] > arr[j]; j--){int temp = arr[j];arr[j] = arr[j - 1];arr[j - 1] = temp;}printArray(arr);}
}
时间复杂度为 O ( n 2 ) O(n^2) O(n2),空间复杂度 O ( 1 ) O(1) O(1)
希尔排序
希尔排序是对插入排序的优化。
int [] array = new int[]{2,5,6,7,1,3}
void printArray(int[] arr){ls = "";foreach(var r in arr){ls += r + ",";}Debug.Log("轮次" + index++ + "数组" + ls);
}
void shellSort(int[] arr, int n){int i = 0;int j = 0;int temp;for(int step = n / 2; step > 0; step = step / 2){for(i = step; i < n; i++){temp = arr[i];for(j = i; j >= step && temp < arr[j - step]; j -= step){arr[j] = arr[j - step];} arr[j] = temp;}printArray(arr);}
}
时间复杂度 O ( n 2 ) O(n^2) O(n2),空间复杂度 O ( 1 ) O(1) O(1),稳定性是不稳定的
堆排序
堆是一颗完全的二叉树,堆中某个节点的值总是不大于(大顶堆)或不小于(小顶堆)其父节点的值
void heapSort(int[] list){// 构建初始堆for(int i = list.Length / 2 - 1; i >= 0; i--){AdjustHeap(list,i,list.Length); // O(n)}// 进行n-1次循环,完成排序for(int i = list.Length - 1; i > 0; i--){ // O(nlogn)// 交换堆顶(最大数)到最后,并把这个节点从二叉树去掉int temp = list[i];list[i] = list[0];list[0] = temp;// 只有堆顶不确定是不是最大,只调整堆顶即可AdjustHeap(list, 0, i);}
}
public void AdjustHeap(int[] array,int parent ,int Length){int temp = array[parent]; // 父节点int child = 2 * parent + 1; // 左子节点while(child < Length){// 找最大的子节点if(child + 1 < Length && array[child] < array[child + 1]){child++;}if(temp >= array[child])break;// 如果子节点较大,交换父子节点array[parent] = array[child];// 子节点作为父节点,进行下一轮比较parent = child;child = 2 * child + 1;}array[parent] = temp;
}
时间复杂度 O ( n log n ) O(n\log n) O(nlogn),空间复杂度 O ( 1 ) O(1) O(1),稳定性是不稳定的
二叉树排序
二叉排序树:空树或满足以下条件
- 如果左子树不空,左子树上的所有节点值均小于根节点的值。如果右子树不空,右子树上的所有节点均大于根节点的值。
- 左、右子树分别为二叉排序树。
遍历顺序:
中序遍历(左根右)、先序遍历(根左右)、后序遍历(左右根)
我们采用中序遍历,也就是说,二叉排序树从小到大的遍历顺序是从最后最左的子节点开始,往上遍历,遍历到父节点的时候,再从下往上,从左往右遍历右子节点,如此往复,遍历完左右节点。
public class BinarySortTreeNode{public int Key{get;set;}public BinarySortTreeNode Left{get;set;}public BinarySortTreeNode Right{get;set;}public BinarySortTreeNode(int key){Key = key;}public void Insert(int key){var tree = new BinarySortTreeNode(key);if(tree.Key <= Key){if(Left == null){Left = tree;}else{Left.Insert(key);}else{if(Right == null){Right = tree;}else{Right.Insert(key);}}}}// 遍历方法(妙啊)public void InorderTraversal(){Left?.InorderTraversal();Debug.Log(Key);Right?.InorderTraversal();}
}public static void BinaryTreeSort(int[] array){var binarySortTreeNode = new BinarySortTreeNode(array[0]);for(int i = 1; i < array.Length; i++){binarySortTreeNode.Insert(array[i]); }binarySortTreeNode.InorderTraversal();
}
时间复杂度 O ( n log n ) O(n\log n) O(nlogn),空间复杂度 O ( n ) O(n) O(n),稳定性是不稳定的
快速排序
参考:https://blog.csdn.net/qq_40941722/article/details/94396010
void Quick_Sort(int *arr, int begin, int end){if(begin > end)return;int tmp = arr[begin];int i = begin;int j = end;while(i != j){while(arr[j] >= tmp && j > i)j--;while(arr[i] <= tmp && j > i)i++;if(j > i){int t = arr[i];arr[i] = arr[j];arr[j] = t;}}arr[begin] = arr[i];arr[i] = tmp;Quick_Sort(arr, begin, i-1);Quick_Sort(arr, i+1, end);
}
function Sort(list, leftIndex, rightIndex)if(leftInfex >= rightIndex) thenreturnendlocal i = leftIndexlocal j = rightIndexlocal baseValue = list[leftIndex]while i < j dowhile i < j and list[j] >= baseValue doj = j - 1endwhile i < j and list[i] <= baseValue doi = i + 1endif j > i thent = list[i];list[i] = list[j];list[j] = t;endendlist[leftIndex] = list[i];list[i] = baseValue;Quick_Sort(list, leftIndex, i-1);Quick_Sort(list, i+1, rightIndex);
end
快速排序最优时间复杂度为O(nlogn),不稳定
归并排序
参考:https://www.bilibili.com/video/BV1Pt4y197VZ/
function sort(arr, startIndex = 0, endIndex = arr.length - 1) {// 递归结束条件:startIndex大于等于endIndex的时候if (startIndex >= endIndex) {return;}// 折半递归let midIndex = parseInt((startIndex + endIndex) / 2);sort(arr, startIndex, midIndex);sort(arr, midIndex + 1, endIndex);// 将两个有序的小数组,合并成一个大数组merge(arr, startIndex, midIndex, endIndex);
}function merge(arr, startIndex, midIndex, endIndex) {// 新建一个大数组let tempArr = [];let p1 = startIndex;let p2 = midIndex + 1;let p = 0;// 比较两个有序小数组的元素,依次放入大数组中while (p1 <= midIndex && p2 <= endIndex) {if (arr[p1] <= arr[p2]) {tempArr[p++] = arr[p1++];} else {tempArr[p++] = arr[p2++];}}// 右侧小数组已排序完毕,左侧小数组还有剩余,将左侧小数组元素依次放入大数组尾部while (p1 <= midIndex) {tempArr[p++] = arr[p1++];}// 左侧小数组已排序完毕,右侧小数组还有剩余,将右侧小数组元素依次放入大数组尾部while (p2 <= endIndex) {tempArr[p++] = arr[p2++];}for (let i = 0; i < tempArr.length; i++) {arr[i + startIndex] = tempArr[i];}
}let arr = [4, 5, 8, 1, 7, 2, 6, 3];
sort(arr);
console.log(arr);
时间复杂度是O(nlogn),稳定
排序算法的使用场景
- 游戏中得分排行榜
- 英雄战力榜列表等
拓扑排序
拓扑排序(Topological Sorting)是一种对有向无环图(Directed Acyclic Graph, DAG)进行排序的算法。在拓扑排序中,节点表示图中的任务或事件,有向边表示任务间的依赖关系。
拓扑排序的目标是将图中的节点按照一定的顺序线性排列,使得所有的依赖关系都得到满足。也就是说,在排序结果中,若存在一条从节点 A 到节点 B 的有向边,那么节点 A 在排序中必须出现在节点 B 之前。
通俗讲解:当同时存在很多件事情需要完成,但是有些事情的完成依赖于另外若干件事情的完成,拓扑排序需要我们排列出事情完成的顺序,这个顺序往往不止一种。
拓扑排序的算法通常使用深度优先搜索(DFS)或广度优先搜索(BFS)来实现。在实际应用中,如果图中存在环路(即有向图中存在回路),则无法进行拓扑排序,因为无法满足所有的依赖关系。因此,拓扑排序算法通常要求应用于有向无环图。
步骤
- 构建图:根据给定的有向图的边关系,构建表示图的数据结构。通常使用邻接表或邻接矩阵表示图。
- 统计节点入度:对于每个节点,统计它的入度(即指向该节点的边的数量)。可以通过遍历图的边关系,记录每个节点的入度。
- 初始化队列:创建一个队列(可以使用队列数据结构),用于存储入度为0的节点。
- 入度为0的节点入队:遍历所有节点,将入度为0的节点加入队列。
- 拓扑排序:从队列中取出一个节点,输出该节点(或保存到结果序列中),并将其相邻节点的入度减1。若某个节点的入度减为0,则将其加入队列。
- 重复步骤5,直到队列为空。如果输出的节点数量与图中的节点数量相同,则拓扑排序成功;否则,图中存在环路,无法进行拓扑排序。
- 输出结果:按照拓扑排序的顺序输出节点,即为拓扑排序的结果。
int topological_sort(LGraph G)
{int i,j;int index = 0;int head = 0; // 辅助队列的头int rear = 0; // 辅助队列的尾int *queue; // 辅组队列int *ins; // 入度数组char *tops; // 拓扑排序结果数组,记录每个节点的排序后的序号。int num = G.vexnum;ENode *node;ins = (int *)malloc(num*sizeof(int)); // 入度数组tops = (char *)malloc(num*sizeof(char));// 拓扑排序结果数组queue = (int *)malloc(num*sizeof(int)); // 辅助队列assert(ins!=NULL && tops!=NULL && queue!=NULL);memset(ins, 0, num*sizeof(int));memset(tops, 0, num*sizeof(char));memset(queue, 0, num*sizeof(int));// 统计每个顶点的入度数for(i = 0; i < num; i++){node = G.vexs[i].first_edge;while (node != NULL){ins[node->ivex]++;node = node->next_edge;}}// 将所有入度为0的顶点入队列for(i = 0; i < num; i ++)if(ins[i] == 0)queue[rear++] = i; // 入队列while (head != rear) // 队列非空{j = queue[head++]; // 出队列。j是顶点的序号tops[index++] = G.vexs[j].data; // 将该顶点添加到tops中,tops是排序结果node = G.vexs[j].first_edge; // 获取以该顶点为起点的出边队列// 将与"node"关联的节点的入度减1;// 若减1之后,该节点的入度为0;则将该节点添加到队列中。while(node != NULL){// 将节点(序号为node->ivex)的入度减1。ins[node->ivex]--;// 若节点的入度为0,则将其"入队列"if( ins[node->ivex] == 0)queue[rear++] = node->ivex; // 入队列node = node->next_edge;}}if(index != G.vexnum){printf("Graph has a cycle\n");free(queue);free(ins);free(tops);return 1;}// 打印拓扑排序结果printf("== TopSort: ");for(i = 0; i < num; i ++)printf("%c ", tops[i]);printf("\n");free(queue);free(ins);free(tops);return 0;
}
其中G是有向图,结构可能如下:
struct ENode
{int ivex; // 该边所指向的顶点的位置struct ENode *next_edge; // 指向下一条弧的指针
} ;struct VNode
{char data; // 顶点的数据ENode *first_edge; // 指向第一条依附该顶点的边的指针
} ;struct struct
{VNode vexs[MAX_VERTEX_NUM]; // 顶点数组int vexnum; // 顶点数量int edgenum; // 边数量
} ;
相关文章:

常用排序算法实现
时间复杂度 O ( 1 ) O(1) O(1) void func1(int n){int count 100;count; } void func2(int n){int count 100;for(int i 0; i < count;i){} } int func3(int n){return n; }O ( n ) O(n) O(n) void func1(int n){int count 100;for(int i 0; i < n;i){count;} …...

使用表单登录方法模拟登录通信人家园,要求发送登录请求后打印出来的用户名下的用户组类别
目标网站:https://www.txrjy.com/forum.php 一、进入网页,右键“检查” 二、输入用户名和密码,点击“登录”,点击“Network”,上划加载项找到蓝色框中的内容 三、点击第一个加载项,找到URL 四、相关代码: …...

Redis 的缓存击穿,穿透,雪崩及其解决方案
1 缓存穿透 什么是缓存穿透? 大量请求的 key 是不合理的,根本不存在于缓存中,也不存在于数据库中 。导致这些请求直接到了数据库上,根本没有经过缓存这一层,对数据库造成了巨大的压力,可能直接就被这么多…...

JWT原理分析——JWT
了解为什么会有JWT的出现? 首先不得不提到一个知识叫做跨域身份验证,JWT的出现就是为了更好的解决这个问题,但是在没有JWT的时候,我们一般怎么做呢?一般使用Cookie和Session,流程大体如下所示:…...

Jprofiler/ VisualVM 定位内存溢出OOM
下载,接受协议下一步下一步,最后选择与IDEA集成OK ej-technologies - Java APM, Java Profiler, Java Installer Builder IDEA配置参数: # F:\study\spring-test\dump 为dump文件保存路径-XX:HeapDumpOnOutOfMemoryError -XX:HeapDumpPathF:\study\spring-test\dumppackage …...

NOIP2023模拟13联测34 competition
题目大意 有一场题目数量为 m m m的比赛,有一个团队想要来参加。 这个团队有 n n n个选手,编号为 i i i的选手能做第 l i ∼ r i l_i \sim r_i li∼ri道题,每题他都有 100 % 100\% 100%的概率做出来。 这个团队会随机派出一只队伍来参…...

Intel oneAPI笔记(2)--jupyter官方文档(oneAPI_Intro)学习笔记
前言 本文是对jupyterlab中oneAPI_Essentials/01_oneAPI_Intro文档的学习记录,包含对SYCL、DPC extends SYCL、oneAPI Programming models等介绍和SYCL代码的初步演示等内容 oneAPI编程模型综述 oneAPI编程模型提供了一个全面而统一的开发人员工具组合࿰…...

用 QT 开发软件会吃官司吗?
之前我写过我们现在使用 QT 开发跨平台软件,有朋友留言,QT 虽好,当心收到律师函。今天就来聊聊这个话题。 在开始这个话题之前,我们先把使用盗版 QT 排除在外,只讨论在合法且遵从版权协议的前提下,能否使用…...

远程运维用什么软件?可以保障更安全?
远程运维顾名思义就是通过远程的方式IT设备等运行、维护。远程运维适用场景包含因疫情居家办公,包含放假期间出现运维故障远程解决,包含项目太远需要远程操作等等。但远程运维过程存在一定风险,安全性无法保障,所以一定要选择靠谱…...

数据结构与算法C语言版学习笔记(2)-线性表、顺序存储结构的线性表
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 数据结构部分的知识框架一、线性表的定义和特点1.定义2.特点 二、线性表的实际案例引入1.案例一:多项式的加减乘除2.案例二:当多项式是稀疏多…...

【vite】vite.defineConfig is not a function/npm无法安装第三方包问题
当使用vite命令 npm init vite-app 项目名称时配置 import vue from vitejs/plugin-vueexport default defineConfig({plugins: [vue()] })会报错vite.defineConfig is not a function 还有就是npm下载的时候也会报错 原因vite插件vitejs/plugin-vue和vite版本问题 解决 调…...

234. 回文链表 --力扣 --JAVA
题目 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。 解题思路 判断链表是否为回文链表取决于链表中各个节点的值,所以可以通过存储各节点的值进行对比判断&…...

【JAVA学习笔记】65 - 文件类,IO流--节点流、处理流、对象流、转换流、打印流
项目代码 https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter19/src/com/yinhai 文件 一、文件,流 文件,对我们并不陌生,文件是保存数据的地方,比如大家经常使用的word文档,txt文件,excel文件..都是文件。它既可以保存一张图片…...

R语言 复习 习题图片
这是日天土申哥不知道从哪淘来的R语言复习知识点图片,大部分内容都是课后习题的答案 加油吧,骚年,考个好分数...

c语言 结构体 简单实例
结构体 简单例子 要求: 结构体保存学生信息操作 代码 #include <stdio.h>//定义结构体 struct student{int ID;char name[20];char sex;char birthday[8];int grade; };int main(){int number;printf("请输入学生个数:");scanf(&quo…...

【ChatGPT】ChatGPT的自定义指令
ChatGPT的自定义指令 关于ChatGPT自定义指令的常见问题解答概述可用性如何使用您的数据自定义指令设置将应用于所有新聊天。启动新聊天可查看更改iOS & AndroidWeb 示例常见问题使用自定义指令的好处字符限制我的ChatGPT数据导出中是否包含自定义指令?当我删除我…...

《哥德尔、艾舍尔、巴赫——集异璧之大成》阅读笔记1
1、谁也不知道非智能行为和智能行为之间的界限在哪里。事实上,认为存在明显界限也许是愚蠢的。但是智能的基本能力还是确定的,它们是: 对于情境有很灵活的反应充分利用机遇弄懂含糊不清或彼此矛盾的信息认识到一个情境中什么是重要的因素&am…...

稳定细胞系构建技术介绍
抗体药物的开发是一个非常复杂的过程,构建适用于工业生产的高表达的稳定细胞株是抗体药工艺开发的起点和基础。一株稳定高产的工程细胞株不仅能显著增加单位体积产量,降低生产成本,还可以降低下游纯化工艺复杂度,确保获得安全&…...

k8s部署srs服务
k8s部署srs服务 项目需要把srs纳入k8s进行管理,需要通过k8s来部署srs服务然后原本的srs可以支持rtmp与webrtc两种,官网查了部署方式,k8s只有最基本的部署方式于是开始研究k8s部署能够正常推拉流的webrtc版本的srs 首先肯定是去官网查有无相关…...

使用Java分割PDF文件
在Java中,我们可以使用iText库来处理PDF文件。iText是一个流行的Java库,用于创建和处理PDF文件。在本篇博客中,我们将介绍如何使用Java分割一个PDF文件为多个小的PDF文件。 1. 引入iText依赖 首先,我们需要在项目中引入iText库的…...

LLM时代中的分布式AI
深度学习相较传统机器学习模型,对算力有更高的要求。尤其是随着深度学习的飞速发展,模型体量也不断增长。于是,前几年,我们看到了芯片行业的百家争鸣和性能指标的快速提升。正当大家觉得算力问题已经得到较大程度的缓解时…...

Zinx框架-游戏服务器开发003:架构搭建-需求分析及TCP通信方式的实现
文章目录 1 项目总体架构2 项目需求2.1 服务器职责2.2 消息的格式和定义 3 基于Tcp连接的通信方式3.1 通道层实现GameChannel类3.1.1 TcpChannel类3.1.2 Tcp工厂类3.1.3 创建主函数,添加Tcp的监听套接字3.1.4 代码测试 3.2 协议层与消息类3.2.1 消息的定义3.2.2 消息…...

如何使用Pyarmor保护你的Python脚本
目录 一、Pyarmor简介 二、使用Pyarmor保护Python脚本 1、安装Pyarmor 2、创建Pyarmor项目 3、添加Python脚本 4、配置执行环境 5、生成保护后的脚本 三、注意事项与未来发展 四、未来发展 五、总结 本文深入探讨了如何使用Pyarmor工具保护Python脚本。Pyarmor是一个…...

【c++】搜索二叉树的模拟实现
搜索二叉树的模拟实现 k模型完整代码 #pragma once namespace hqj1 {template<class K>struct SBTreeNode{public://这里直接用匿名对象作为缺省参数SBTreeNode(const K& key K()):_key(key), _cleft(nullptr), _cright(nullptr){}public:K _key;SBTreeNode* _cle…...

Kubeadm - K8S1.20 - 高可用集群部署(博客)
这里写目录标题 Kubeadm - K8S1.20 - 高可用集群部署一.环境准备1.系统设置 二.所有节点安装docker三.所有节点安装kubeadm,kubelet和kubectl1.定义kubernetes源2.高可用组件安装、配置 四.部署K8S集群五.问题解决1.加入集群的 Token 过期2.master节点 无法部署非系…...

515. 在每个树行中找最大值
描述 : 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。 题目 : LeetCode 在每个树行中找最大值 : 515. 在每个树行中找最大值 分析 : 这里其实就是在得到一层之后使用一个变量来记录当前得到的最大值 , 懂了前面的几道这就是小菜 解析 : /…...

基于springboot+vue的图书馆管理系统
图书馆管理系统 springboot32阿博图书馆管理系统 源码合集: www.yuque.com/mick-hanyi/javaweb 源码下载:博主私 摘 要 随着社会的发展,计算机的优势和普及使得阿博图书馆管理系统的开发成为必需。阿博图书馆管理系统主要是借助计算机&…...

诊断刷写流程中使用到的诊断服务
10 01:诊断刷写完成后让目标ECU重置或让整车网络中其他ECU切换回默认会话 10 02:设置外部编程请求标志位或切换到编程会话(诊断刷写需要在编程会话下进行) 10 03:让目标ECU切换到扩展会话,以便进行其他诊断…...

pytorch 中 nn.Conv2d 解释
1. pytorch nn.Con2d 中填充模式 torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride1, padding0, dilation1, groups1, biasTrue, padding_mode‘zeros’, deviceNone, dtypeNone) 1.1 padding 参数的含义 首先 ,padd N, 代表的是 分别在 上下&…...

漏刻有时百度地图API实战开发(2)文本标签显示和隐藏的切换开关
项目说明 在百度地图开发的过程中,如果遇见大数据量POI标注展示或在最佳视野展示时,没有文本标签,会不清楚具体标注的代表的意义;如果同时显示大量的文本标签,又会导致界面杂乱且无法清晰查看,因此&#x…...