当前位置:网站首页>Application of merging and sorting thought
Application of merging and sorting thought
2022-07-23 23:22:00 【Hua Weiyun】
Preface
When learning sorting algorithm , First acquaintance merge sort , How can this sort be so difficult from the amount of code . In fact, the idea of merging and sorting is very simple : Put two ( Or more than two ) An ordered table is merged into a new ordered table , That is, the sequence to be sorted is divided into several subsequences , Every subsequence is ordered . Then we combine the ordered subsequences into the whole ordered sequences . Merge sort is an effective sort algorithm based on merge operation . The algorithm adopts Divide and conquer method (Divide and Conquer) A very typical application . The following is a systematic learning with a programming example .
Time complexity :O(nlogn)
Spatial complexity :O(n)
stability : Stable
Reverse pairs in arrays
Title Description
Two numbers in an array , If the number in front is greater than the number in the back , Then these two numbers form a reverse order pair . Enter an array , Find the total number of reverse pairs in this array P. And will P Yes 1000000007 The output of the result of taking the mold . The output P%1000000007.
Input description :
Make sure that the same number is not in the input array
Data range :
about %50 The data of ,size<=10^4
about %75 The data of ,size<=10^5
about %100 The data of ,size<=2*10^5
Input example :
1,2,3,4,5,6,7,0
Output example :
7
The solution idea here is : First, divide the array into subarrays , Count the number of inverse pairs in the word array ; Then count the number of reverse pairs between two adjacent sub arrays . In the process of statistical reverse pairing , You also need to sort the arrays . It's not hard to find out , This sort process is actually merge sort .
Recursion is used in merging and sorting , In fact, I'm not interested in recursion . Recursion makes the code look cleaner and simpler , But because it uses the stack , The memory size of the stack is certain , Therefore, when the recursion depth is too large , There will be stack overflow StackOverflow Abnormal conditions of . The recursive method can be replaced by the non recursive or circular method .
Merge sort original algorithm
边栏推荐
- fl studio 20.9更新中文版宿主DAW数字音频工作站
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- Diabetes genetic risk testing challenge baseline
- The Minesweeper game
- Chinese NFT? NFR was born
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- After reading this article, thoroughly understand grpc!
- Ways to improve the utilization of openeuler resources 01: Introduction
- DHCP: prevent rogue DHCP server in the network
- 在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
猜你喜欢

Analytic hierarchy process (matlab)

strncat() strncmp()
![[ CTF ]天格战队WriteUp-首届数字空间安全攻防大赛(初赛)](/img/61/5547822b782043672b626f6b86d304.png)
[ CTF ]天格战队WriteUp-首届数字空间安全攻防大赛(初赛)

(CVPR-2022)BiCnet

What is the difference between go run, go build and go install

AutoCAD advanced operation
![[leetcode ladder] linked list · 203 remove linked list elements](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode ladder] linked list · 203 remove linked list elements

Chinese NFT? NFR was born

Grey prediction (matlab)

Classification model - logistic regression, Fisher linear discriminant (SPSS)
随机推荐
TOPSIS method (matlab)
Analytic hierarchy process (matlab)
A deserialized CTF question sharing
难怪国内企业ERP应用效果普遍不理想
Light up the LED light of little bear patting learning
[leetcode ladder] linked list · 203 remove linked list elements
疑似未系安全带 林志颖伤势相对稳定
[ CTF ]天格战队WriteUp-首届数字空间安全攻防大赛(初赛)
[数组]NC95 数组中的最长连续子序列-较难
Quickly learn to use file permissions
Excel password related
[leetcode ladder] the penultimate node in the 022 linked list
TAP 系列文章6 | TAP的应用模型
浅析基于NVR技术的视频能力与未来发展趋势
Chinese NFT? NFR was born
D1-h development board - Introduction to Nezha development
The role of physical layer, link layer, network layer, transport layer and application layer of tcp/ip model of internet protocol stack
Lu Xia action | Source Kai Digital: Existing Mode or open source innovation?
SOLIDWORK learning notes: Sketch geometric relationships and editing
mysqlbinlog命令介绍(远程拉取binlog日志)