当前位置:网站首页>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
边栏推荐
- Mobile, telecom and Unicom: fancy solution of 5g to B
- Go language multiple return values and return error types
- TOPSIS method (matlab)
- J9 number theory: how can we overcome the fomo phenomenon in the digital industry?
- FL Studio 20.9 update Chinese version host Daw digital audio workstation
- [数组]NC95 数组中的最长连续子序列-较难
- The basic syntax of go language (variables, constants, basic data types, for, switch, case, array, slice, make, new, map)
- Notes on network segment CIDR
- Analysis of mobile semantics and perfect forwarding
- Tap series article 4 | backstage based tap developer portal
猜你喜欢

Series of articles | the way to advance the microservice architecture in the cloud native era - best practices of microservice splitting

USB Foundation

BGP basic experiment

1000 okaleido tiger launched binance NFT, triggering a rush to buy

Preparation for raspberry pie 3B serial port login

STM32F4查看系统各部分频率

unity visual studio2019升级到2022版本(扔掉盗版红渣)

Principal component analysis (matlab)

Extract any page number in PDF file with itextpdf

Redis管道技术/分区
随机推荐
The basic syntax of go language (variables, constants, basic data types, for, switch, case, array, slice, make, new, map)
Analysis of video capability and future development trend based on NVR Technology
Preparation for raspberry pie 3B serial port login
Quickly learn to use file permissions
Tap series article 8 | tap Learning Center - learn through hands-on tutorials
Smart IOT source code with configuration IOT source code industrial IOT source code: support sensor analysis services, real-time data collection and remote control
Software architecture
FreeRTOS personal notes - suspend / unhook tasks
Learning MySQL is enough
Three network modes of VMware virtual machine
Analysis of mobile semantics and perfect forwarding
浅析基于NVR技术的视频能力与未来发展趋势
Diabetes genetic risk testing challenge baseline
js把数字转大写
Lin Zhiying's injury is relatively stable
TAP 系列文章7 | 易于管理的流水线配置
Tap series article 9 | application development accelerator
砺夏行动|源启数字化:既有模式,还是开源创新?
[unity3d daily bug] unity3d solves "the type or namespace name" XXX "cannot be found (are you missing the using directive or assembly reference?)" Etc
The canfd/can interface offline burning operation instructions of h7-tool have been updated (2022-07-12)