当前位置:网站首页>Recognize the ordering of O (nlogn)
Recognize the ordering of O (nlogn)
2022-06-27 07:45:00 【KevinJune】
1 Merge sort
1. The whole is a simple recursion , Order on the left 、 Order on the right 、 Make it whole and orderly
2. In the process of making it whole and orderly, we use the method of exclusive order ( Left and right comparison , Put the smaller value into an external array , Then the called data pointer moves down one bit , In case of equality, the data on the left is taken by default , If the array data on one side is extracted , Save all the remaining data to the external array )
3. utilize master Formula to solve the time complexity
4. The essence of merging and sorting
Time complexity O(N*logN), Extra space complexity O(N)

2 Extension of merge sort
Small sum problem and reverse order pair problem
Small and problem
In an array , The number smaller than the current number on the left of each number is added up , It's called the small sum of this array . Find the small sum of an array .
Example :[1, 3, 4, 2, 5]
1 On the left 1 Small number , No, ;3 On the left 3 Small number ,1;4 On the left 4 Small number ,1、3;2 On the left 2 Small number ,1;5 On the left 5 Small number ,1、3、4、2;
So Xiaohe is 1+1+3+1+1+3+4+2 = 16
The calculation process is similar to that of merge sorting , However, it needs to be sorted and small at the same time , And when the left and right data are equal, you need to copy the data on the right to the outer order array first .
Reverse order pair problem
In an array , If the number on the left is larger than the number on the right , Then fold two numbers to form a reverse order pair , Please print all pairs in reverse order .
3 The Dutch flag
Question 1
Given an array arr, And a number num, Please put less than or equal to num The number of is on the left side of the array , Greater than num To the right of the array . Requires extra space complexity O(1), Time complexity O(N)
Question two
Given an array arr, And a number num, Please put less than num The number of is on the left side of the array , be equal to num The number of is in the middle of the array , Greater than num To the right of the array . Requires extra space complexity O(1), Time complexity O(N)
4 Quick sort without improvement
1. Take the last number in the array range as the partition value , Then divide the array into three parts through the Dutch flag problem ;
left < Division value , middle == Division value , On the right side > Division value
2. For the left range and the right range , Recursive execution
analysis
1. The closer the partition value is to both sides , The more complex ; The closer the partition value is to the middle , The lower the complexity
2. It's easy to give the worst example , Therefore, the time complexity of quick sort without improvement is O(N^2)
5 Random quick sort ( Improved quick sorting )
1. In the array range , Select a number as the partition value with equal probability , Then divide the array into three parts through the Dutch flag ;
left < Division value , middle == Division value , On the right side > Division value
2. For the left range and the right range , Recursive execution
3. The time complexity is O(N*logN)
边栏推荐
- 野風藥業IPO被終止:曾擬募資5.4億 實控人俞蘠曾進行P2P投資
- js求所有水仙花数
- R language analyzing wine data
- What is futures reverse documentary?
- 2. QT components used in the project
- (resolved) NPM suddenly reports an error cannot find module 'd:\program files\nodejs\node_ modules\npm\bin\npm-cli. js‘
- 【11. 二维差分】
- 碎煤机crusher
- JS to judge the odd and even function and find the function of circular area
- MySQL
猜你喜欢

Online text digit recognition list summation tool

【批处理DOS-CMD命令-汇总和小结】-环境变量、路径变量、搜索文件位置相关指令——set、path、where,cmd命令的路径参数中有空格怎么办

js中判断成绩是否合格,范围在0-100,否则重新输入
![[compilation principles] review outline of compilation principles of Shandong University](/img/a6/b522a728ff21085411e7452f95872a.png)
[compilation principles] review outline of compilation principles of Shandong University

JS example print the number and sum of multiples of all 7 between 1-100

File and multipartfile overview

Win10 how to manage startup items?

Basic knowledge | JS Foundation

Cookie加密6

2、项目使用的QT组件
随机推荐
再见了,敏捷Scrum
【批处理DOS-CMD命令-汇总和小结】-输出/显示命令——echo
ACM课程学期总结
认识O(NlogN)的排序
Bean copy details
MySQL about auto increment sum cannot be empty
Speech signal processing - concept (II): amplitude spectrum (STFT spectrum), Mel spectrum [the deep learning of speech mainly uses amplitude spectrum and Mel spectrum] [extracted with librosa or torch
JS print 99 multiplication table
js输出1-100之间所有的质数并求总个数
One person manages 1000 servers? This automatic operation and maintenance tool must be mastered
Cookie加密6
安装jenkins
移动安全工具-jad
Hutool symmetric encryption
log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.
What is a flotation machine?
MySQL
Yarn create vite reports an error 'd:\program' which is neither an internal or external command nor a runnable program or batch file
【批处理DOS-CMD命令-汇总和小结】-将文件夹映射成虚拟磁盘——subst
c#的初步认识