Home Backend Development PHP Tutorial php 归并排序 数组交集_PHP

php 归并排序 数组交集_PHP

Jun 01, 2016 pm 12:16 PM
merge sort Array intersection

复制代码 代码如下:
$a=array('1','2','3','4','22');
$b=array('1','3','4','11','22','23');
f($a, $b, 5, 6, $t);
print_r($t);
function f(&$a, &$b, $n, $m, &$t){
$i=0;$j=0;
while($iif($a[$i]==$b[$j]){
echo $a[$i]." ";//交集
$t[]=$a[$i++];
$t[]=$b[$j++];
}elseif($a[$i]>$b[$j]){
$t[]=$b[$j++];
}else{
$t[]=$a[$i++];
}
}
while($i$t[]=$a[$i++];
}
while($j$t[]=$b[$j++];
}
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C/C++ program written using the merge sort algorithm to calculate reverse numbers in an array C/C++ program written using the merge sort algorithm to calculate reverse numbers in an array Aug 25, 2023 pm 07:33 PM

The inverted representation of an array; how many changes are required to convert the array into its sorted form. When the array is already sorted, 0 reversals are required, while in other cases, if the array is reversed, the maximum number of reversals will be reached. In order to solve this problem, we will follow the merge sort method to reduce the time complexity and use the divide and conquer algorithm. Input Asequenceofnumbers.(1,5,6,4,20). Output the number of reversals required to sort the numbers in ascending order. Herethenumberofinversionsare2.Firstinversion:(1,5,4,6,20)Secondinversion:(1,4,5,6,20)algorithm merge

Comparing the performance of different array intersection and union methods in PHP Comparing the performance of different array intersection and union methods in PHP May 03, 2024 pm 02:18 PM

The analysis results show that: for simple intersection operations, array_intersect() has the best performance; for custom comparison intersection operations, array_uintersect() has the best performance; for simple union operations, array_union() has the best performance; for custom comparison union operations , array_uunion() has the best performance.

How to implement merge sorting in php How to implement merge sorting in php Oct 21, 2022 am 09:30 AM

How to implement merge sorting in PHP: 1. Create a PHP sample file; 2. Define the "public function handle(){...}" method; 3. Use "private function mergeSort($a, $lo, $hi)" {...}" method to gradually decompose the data; 4. Use the "merge" method to sort the decomposed data and then merge them together.

Deep Dive: Array Intersection Operations in Golang Deep Dive: Array Intersection Operations in Golang Apr 04, 2024 am 09:45 AM

There are three ways to get the intersection of arrays in Golang: use Sprint and Scanf of the fmt package to convert the array into a string and find the elements in one string that contain another string; use the map package to create a map with keys in another array elements and check if the element is in the map; use the math/big package to store the array as a big integer and use logical operators for intersection calculations.

Detailed explanation of merge sort algorithm in PHP Detailed explanation of merge sort algorithm in PHP Jul 08, 2023 pm 05:03 PM

Detailed explanation of the merge sort algorithm in PHP Introduction: Sorting is one of the common basic problems in computer science. The orderly arrangement of data can improve the efficiency of retrieval, search and modification operations. Among sorting algorithms, merge sort is a highly efficient and stable algorithm. This article will introduce the merge sort algorithm in PHP in detail, with code examples. Principle of Merge Sort Merge sort is a divide-and-conquer algorithm that divides the array to be sorted into two sub-arrays, merges and sorts the two sub-arrays respectively, and then merges the sorted sub-arrays into one

How to implement the merge sort algorithm in C# How to implement the merge sort algorithm in C# Sep 19, 2023 am 09:45 AM

How to implement the merge sort algorithm in C# Merge sort is a classic sorting algorithm based on the divide-and-conquer idea. It completes sorting by dividing a large problem into multiple small problems, then gradually solving the small problems and merging the results. The following will introduce how to implement the merge sort algorithm in C# and provide specific code examples. The basic idea of ​​merge sort is to split the sequence to be sorted into multiple subsequences, sort them separately, and then merge the sorted subsequences into an ordered sequence. The key to this algorithm is to implement the splitting and merging operations of subsequences.

How to implement merge sort algorithm using java How to implement merge sort algorithm using java Sep 19, 2023 am 11:33 AM

How to use Java to implement the merge sort algorithm Introduction: Merge sort is a classic sorting algorithm based on the divide and conquer method. The idea is to divide the array to be sorted into smaller sub-arrays layer by layer, and then merge the sub-arrays in sequence through the merge operation. Merge into a sorted overall array. In this article, we will introduce in detail how to implement the merge sort algorithm using Java and provide specific code examples. Algorithm steps: The merge sort algorithm mainly includes three steps: splitting, merging and sorting. Split: First, we need

How to use divide and conquer method to implement merge sort algorithm in PHP and improve sorting efficiency? How to use divide and conquer method to implement merge sort algorithm in PHP and improve sorting efficiency? Sep 19, 2023 pm 02:10 PM

How to use divide and conquer method to implement merge sort algorithm in PHP and improve sorting efficiency? Merge sort is an efficient sorting algorithm. It uses the idea of ​​divide and conquer method to divide the array to be sorted into two parts, sort the two sub-arrays respectively, and then merge the two sorted sub-arrays into one. ordered array. Merge sort can stably turn an unsorted array into an ordered array by continuously breaking the problem into smaller sub-problems and combining the solutions to the sub-problems. In PHP, implement the merge sort algorithm and improve sorting efficiency

See all articles