当前位置:网站首页>认识O(NlogN)的排序
认识O(NlogN)的排序
2022-06-27 07:31:00 【KevinJune】
1 归并排序
1. 整体就是一个简单递归,左边排好序、右边排好序、让其整体有序
2. 让其整体有序的过程里用了排外序方法(左右两个对比,将数值较小的放进外部的一个数组内,然后被调用的数据指针往下移一位,相等时默认取左边的数据,如果一侧的数组数据被提取完成,将剩下的数据全部保存到外部数组即可)
3. 利用master公式来求解时间复杂度
4. 归并排序的实质
时间复杂度O(N*logN), 额外空间复杂度O(N)

2 归并排序的扩展
小和问题和逆序对问题
小和问题
在一个数组中,每一个数左边比当前数小的数累加起来,叫做这个数组的小和。求一个数组的小和。
例子:[1, 3, 4, 2, 5]
1左边比1小的数,没有;3左边比3小的数,1;4左边比4小的数,1、3;2左边比2小的数,1;5左边比5小的数,1、3、4、2;
所以小和为 1+1+3+1+1+3+4+2 = 16
计算过程同归并排序类似,不过是需要在排序的同时也要求小和,以及当左右两个数据相等时需要先将右方的数据复制到外序数组。
逆序对问题
在一个数组中,左边的数如果比右边的数大,则折两个数构成一个逆序对,请打印所有逆序对。
3 荷兰国旗问题
问题一
给定一个数组arr,和一个数num,请把小于等于num的数放在数组的左边,大于num的数放在数组的右边。要求额外空间复杂度O(1),时间复杂度O(N)
问题二
给定一个数组arr,和一个数num,请把小于num的数放在数组的左边,等于num的数放在数组的中间,大于num的数放在数组的右边。要求额外空间复杂度O(1),时间复杂度O(N)
4 不改进的快速排序
1. 把数组范围中的最后一个数作为划分值,然后把数组通过荷兰国旗问题分成三个部分;
左侧<划分值,中间==划分值,右侧>划分值
2. 对左侧范围和右侧范围,递归执行
分析
1. 划分值越靠近两侧,复杂度越高;划分值就越靠近中间,复杂度越低
2. 可以轻而易举的举出最差的例子,所以不改进的快速排序时间复杂度为O(N^2)
5 随机快速排序(改进的快速排序)
1.在数组范围中,等概率的选取一个数作为划分值,然后把数组通过荷兰国旗分体分成三个部分;
左侧<划分值,中间==划分值,右侧>划分值
2. 对左侧范围和右侧范围,递归执行
3. 时间复杂度为O(N*logN)
边栏推荐
- R language consumption behavior statistics based on association rules and cluster analysis
- 1-4 decimal representation and conversion
- JS use switch to output whether the result is qualified
- 移动安全工具-jad
- 程序人生 - 程序员三十五岁瓶颈你怎么看?
- 2022 cisp-pte (II) SQL injection
- RNA SEQ data analysis in R - investigate differentially expressed genes in the data!
- Custom palette for ggplot2
- What is a flotation machine?
- MySQL
猜你喜欢

JS print 99 multiplication table
![log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.](/img/2c/425993cef31dd4c786f9cc5ff081ef.png)
log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.

正斜杠反斜杠的由来

【软件工程】山东大学软件工程复习提纲

Multi table associated query -- 07 -- hash join

语音信号特征提取流程:输入语音信号-分帧、预加重、加窗、FFT->STFT谱(包括幅度、相位)-对复数取平方值->幅度谱-Mel滤波->梅尔谱-取对数->对数梅尔谱-DCT->FBank->MFCC

JS performance reward and punishment examples

js用while循环计算假如投资多年的利率为5%,试求从1000块增长到5000块,需要花费多少年

Interviewer: you use Lombok every day. What is its principle? I can't answer

Cookie加密6
随机推荐
JS to print prime numbers between 1-100 and calculate the total number of optimized versions
用XGBoost迭代读取数据集
Installation and functions of uview
什么是浮选机?
Speech signal processing - concept (4): Fourier transform, short-time Fourier transform, wavelet transform
log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.
多表联查--07--- Hash join
Cookie encryption 6
JDBC reads MySQL data list
语音信号特征提取流程:输入语音信号-分帧、预加重、加窗、FFT->STFT谱(包括幅度、相位)-对复数取平方值->幅度谱-Mel滤波->梅尔谱-取对数->对数梅尔谱-DCT->FBank->MFCC
Coal crusher
请问如何将数据从oracle导入fastDFS?
The first part of the construction of the defense system of attack and defense exercise is the introduction and the four stages of Defense
volatile 和 synchronized 到底啥区别?
Coggle 30 Days of ML 7月竞赛学习
VNC Viewer方式的远程连接树莓派
Cookie encryption 7 fidder analysis phase
window右键管理
Goodbye, agile Scrum
One person manages 1000 servers? This automatic operation and maintenance tool must be mastered