当前位置:网站首页>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
边栏推荐
- 13. Roman to integer
- Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework
- Tap series article 5 | cloud native build service
- Remember an experience of being cheated by the Internet
- What is the difference between go run, go build and go install
- [leetcode ladder] linked list · 203 remove linked list elements
- ES6箭头函数的使用
- As a developer, you have to know the three performance testing tools JMeter, API and jmh user guide
- 【Unity3D日常BUG】Unity3D解决“找不到类型或命名空间名称“XXX”(您是否缺少using指令或程序集引用?)”等问题
- Analysis of video capability and future development trend based on NVR Technology
猜你喜欢

浅析基于NVR技术的视频能力与未来发展趋势

Tap series article 8 | tap Learning Center - learn through hands-on tutorials

Finding all paths between two points in a directed graph

2、 Digital logic functional unit

DHCP: prevent rogue DHCP server in the network

BGP basic experiment

Getting started database days2

Niuke C basic exercises

FL Studio 20.9 update Chinese version host Daw digital audio workstation

Mongodb - Introduction to the use of $exists and the combination of $ne, $nin, $nor, $not in query statements
随机推荐
Build your own target detection environment, model configuration, data configuration mmdetection
Tensorflow one layer neural network training handwritten digit recognition
About: enable delivery optimization in enterprise LAN
ES6 other syntax and extended syntax summary
FL Studio 20.9 update Chinese version host Daw digital audio workstation
汇编语言伪指令详解(附实例)
The I2C interface mode offline burning operation method of h7-tool has been released (2022-07-16)
mysqlbinlog命令介绍(远程拉取binlog日志)
Chinese NFT? NFR was born
Finding all paths between two points in a directed graph
48: Chapter 5: develop admin management service: 1: create sub project [imooc news dev Service Admin], management service module;
The Minesweeper game
FreeRTOS personal notes - create / delete dynamic tasks, start scheduler
Sql156 average completion rate of each video
礪夏行動|源啟數字化:既有模式,還是開源創新?
Tap series article 7 | easy to manage pipeline configuration
Utilisation des fonctions fléchées es6
The basic syntax of go language (variables, constants, basic data types, for, switch, case, array, slice, make, new, map)
TAP 系列文章5 | 云原生构建服务
Lixia action | Yuanqi Digitalization: existing mode or open source innovation?