当前位置:网站首页>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)
边栏推荐
- JDBC事务提交事例
- How to bind SQL statements to web buttons
- win10-如何管理开机启动项?
- volatile 和 synchronized 到底啥区别?
- 【c ++ primer 笔记】第3章 字符串、向量和数组
- JS print 99 multiplication table
- Online text digit recognition list summation tool
- The first part of the construction of the defense system of attack and defense exercise is the introduction and the four stages of Defense
- Error in idea connection database
- MySQL about auto increment sum cannot be empty
猜你喜欢

One person manages 1000 servers? This automatic operation and maintenance tool must be mastered
![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.

JS, and output from small to large

File and multipartfile overview

Stream常用操作以及原理探索

js中判断成绩是否合格,范围在0-100,否则重新输入

【c ++ primer 笔记】第4章 表达式

Remote connection raspberry pie in VNC Viewer Mode

L'introduction en bourse de Wild Wind Pharmaceutical a pris fin: Yu pinzeng, qui avait l'intention de lever 540 millions de RMB, a effectué un investissement P2P.
![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](/img/b3/6c8d9fc66e2a4dbdc0dd40179266d3.png)
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 performance reward and punishment examples
Stream常用操作以及原理探索
野風藥業IPO被終止:曾擬募資5.4億 實控人俞蘠曾進行P2P投資
什么是期货反向跟单?
File and multipartfile overview
Online text digit recognition list summation tool
JS output shape
2、项目使用的QT组件
突破从0到1后,鲜花电商2.0时代怎么走?
JS example print the number and sum of multiples of all 7 between 1-100
MySQL
Win10 how to manage startup items?
js用while循环计算假如投资多年的利率为5%,试求从1000块增长到5000块,需要花费多少年
Set the address book function to database maintenance, and add user name and password
Publications under nature, science and cell
Cookie encryption 6
移动安全工具-jad
How can I import data from Oracle into fastdfs?
University database mysql
第6届蓝桥杯