PHP implements two-way merge sort
<span> 1</span><span>$arr</span> = [9, 43, 12, 0, 87, 1<span>]; </span><span> 2</span><span>function</span> merge_sort(&<span>$arr</span><span>){ </span><span> 3</span> _merge_sort(<span>$arr</span>, <span>$arr</span>, 0, <span>count</span>(<span>$arr</span>) - 1<span>); </span><span> 4</span><span>} </span><span> 5</span><span> 6</span><span>function</span> _merge_sort(&<span>$s_arr</span>, &<span>$d_arr</span>, <span>$i</span>, <span>$j</span><span>){ </span><span> 7</span><span>if</span>(<span>$i</span> > <span>$j</span><span>){ </span><span> 8</span><span>return</span><span>; </span><span> 9</span><span> } </span><span>10</span><span>if</span>(<span>$i</span> == <span>$j</span><span>){ </span><span>11</span><span>echo</span> 'aa'<span>; </span><span>12</span><span>$d_arr</span>[<span>$i</span>] = <span>$s_arr</span>[<span>$i</span><span>]; </span><span>13</span><span>return</span><span>false</span><span>; </span><span>14</span><span> } </span><span>15</span><span>$tmp_arr</span> = <span>array</span><span>(); </span><span>16</span><span>$m</span> = <span>intval</span>((<span>$i</span> + <span>$j</span>)/2<span>); </span><span>17</span><span>echo</span><span>$m</span><span>; </span><span>18</span><span>if</span>(<span>$i</span> <= <span>$m</span><span>){ </span><span>19</span> _merge_sort(<span>$s_arr</span>, <span>$tmp_arr</span>, <span>$i</span>, <span>$m</span><span>); </span><span>20</span><span> } </span><span>21</span><span>if</span>(<span>$m</span>+1 <= <span>$j</span><span> ){ </span><span>22</span> _merge_sort(<span>$s_arr</span>, <span>$tmp_arr</span>, <span>$m</span>+1, <span>$j</span><span>); </span><span>23</span><span> } </span><span>24</span> merge(<span>$tmp_arr</span>, <span>$d_arr</span>, <span>$i</span>, <span>$m</span>, <span>$j</span><span>); </span><span>25</span><span>} </span><span>26</span> //$s_arr中的$start到$m与$m到$end两个序列都是有序的,将这两个序列合并到$d_arr里面 <span>27</span><span>function</span> merge(&<span>$s_arr</span>, &<span>$d_arr</span>, <span>$start</span>, <span>$m</span>, <span>$end</span><span>){ </span><span>28</span><span>$i</span> = <span>$start</span>; <span>$j</span> = <span>$m</span>+1;<span>$d_i</span> = <span>$i</span><span>; </span><span>29</span><span>while</span>(<span>$i</span> <= <span>$m</span> && <span>$j</span> <= <span>$end</span><span>){ </span><span>30</span><span>if</span>(<span>$s_arr</span>[<span>$i</span>] > <span>$s_arr</span>[<span>$j</span><span>]){ </span><span>31</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$i</span>++<span>]; </span><span>32</span><span>//</span><span>$i++;</span><span>33</span> }<span>else</span><span>{ </span><span>34</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$j</span>++<span>]; </span><span>35</span><span> } </span><span>36</span><span> } </span><span>37</span><span>while</span>(<span>$i</span> <= <span>$m</span><span>){ </span><span>38</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$i</span>++<span>]; </span><span>39</span><span>//</span><span>$i++;</span><span>40</span><span> } </span><span>41</span><span>while</span> (<span>$j</span> <= <span>$end</span><span>) { </span><span>42</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$j</span>++<span>]; </span><span>43</span><span> } </span><span>44</span> }
The above introduces the implementation of two-way merge sorting in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

Uniapp is a cross-platform development framework. Its powerful cross-end capabilities allow developers to develop various applications quickly and easily. It is also very simple to implement drag-and-drop sorting and drag-and-drop operations in Uniapp, and it can support drag-and-drop operations of a variety of components and elements. This article will introduce how to use Uniapp to implement drag-and-drop sorting and drag-and-drop operations, and provide specific code examples. The drag-and-drop sorting function is very common in many applications. For example, it can be used to implement drag-and-drop sorting of lists, drag-and-drop sorting of icons, etc. Below we list

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 > /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

What is AMP Coin? The AMP token was created by the Synereo team in 2015 as the main trading currency of the Synereo platform. AMP token aims to provide users with a better digital economic experience through multiple functions and uses. Purpose of AMP Token The AMP Token has multiple roles and functions in the Synereo platform. First, as part of the platform’s cryptocurrency reward system, users are able to earn AMP rewards by sharing and promoting content, a mechanism that encourages users to participate more actively in the platform’s activities. AMP tokens can also be used to promote and distribute content on the Synereo platform. Users can increase the visibility of their content on the platform by using AMP tokens to attract more viewers to view and share

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

Example In this example, we first look at the usage of list.sort() before continuing. Here, we have created a list and sorted it in ascending order using sort() method - #CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList .sort(
