


How to implement array merge sorting and count the number of reverse-order pairs in PHP (code)
The content of this article is about how to implement array merging and sorting in PHP and calculate the number of reverse-order pairs (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Two numbers in the array, if the previous number is greater than the following number, then the two numbers form a reverse-order pair. Input an array and find the total number of reverse-order pairs P in the array. And output the result of P modulo 1000000007. That is, output P 00000007
1. Array merge sort
2. Merge sort When comparing the sizes of elements in the left and right heap arrays, count and compare backwards, because the left heap is the first if it is worse than the right heap The first is the largest, then it is larger than everything in the right heap
mergeSort if left<right mid=[(p+r)/2] mergeSort(arr,left,mid,temp) mergeSort(arr,mid+1,right,temp) merge(arr,left,mid,right,temp) merge(arr,left,mid,right,temp) i=mid j=right t=right while i<=mid && j<=right if arr[i<arr[j] temp[t--]=arr[i--] else count+=mid-i+1 temp[t--]=arr[j--] while i<=mid temp[t--]=arr[i] while j<=right temp[t--]=arr[j]
The temporary array is copied back to the original array
function InversePairs($data) { $num=0; $temp=array(); mergeSort($data,0,count($data)-1,$temp,$num); $num%=1000000007; return $num; } //1.利用分治法思想,递归的切分排序元素 function mergeSort(&$A,$left,$right,$temp,&$num){ //2.最左只能小于最右,等于的时候就一个元素,大于是不可能的 if($left<$right){ //3.获取中间的元素 $mid=intval(($left+$right)/2); //4.递归左半区 mergeSort($A,$left,$mid,$temp,$num); //5.递归右半区 mergeSort($A,$mid+1,$right,$temp,$num); //6.合并两个有序数组为一个有序数组 merge($A,$left,$mid,$right,$temp,$num); } } function merge(&$A,$left,$mid,$right,$temp,&$num){ //7.左堆起始 $i=$left; //8.右堆起始 $j=$mid+1; //9.临时数组起始 $t=0; //10.左右堆数组都没到末尾 while($i<=$mid && $j<=$right){ //11.左堆小于等于右堆时 if($A[$i]<$A[$j]){ //12.左堆赋给临时数组,索引加1 $temp[$t++]=$A[$i++]; }else{ $num+=$mid-$i+1; //13.右堆赋给临时数组,索引加1 $temp[$t++]=$A[$j++]; } } //14.左堆剩余的全部加进临时数组 while($i<=$mid){ $temp[$t++]=$A[$i++]; } //15.右堆剩余全部加进临时数组 while($j<=$right){ $temp[$t++]=$A[$j++]; } //16.临时数组的元素重新赋回原数组 for($i=0;$i<$t;$i++){ $A[$left+$i]=$temp[$i]; } } $A=[364,637,341,406,747,995,234,971,571,219,993,407,416,366,315,301,601,650,418,355,460,505,360,965,516,648,727,667,465,849,455,181,486,149,588,233,144,174,557,67,74 6,550,474,162,268,142,463,221,882,576,604,739,288,569,256,936,275,401,497,82,935,983,583,523,697,478,147,795,380,973,958,115,773,870,259,655,446,863,735,784,3,671,43 3,630,425,930,64,266,235,187,284,665,874,80,45,848,38,811,267,575]; $m=InversePairs($A); var_dump($m);
The above is the detailed content of How to implement array merge sorting and count the number of reverse-order pairs in PHP (code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
