php bubble sort quick sort
/******
1) Bubble sorting: Exchange values in pairs, with the smallest value on the left, just like the lightest bubble on the top.
2) Exchange the entire column of numbers once, with the smallest number on the far left. Each time, you can get the smallest number among the remaining numbers. The "popped" numbers form an ordered interval, and the remaining numbers are The values form an unordered interval, and the value of each element in the ordered interval is smaller than that of the unordered interval.
3) Quick sort: base number, left and right arrays, recursive call, merge.
4)Insertion sort: The sorting interval is divided into two parts, the left side is ordered and the right side is unordered. Take the first element from the right interval and insert it into the left interval. If this element is larger than the rightmost element of the left interval, leave it where it is. If this element is smaller than the rightmost element in the left range, it will be inserted at the original position of the rightmost element. At the same time, the rightmost element will be shifted one position to the right. The calculator will be reduced by one and will be compared with the previous element again until the previous element is smaller than the previous element. To insert elements as small as possible, repeat the above steps.
6) Pay attention to the processing of interval endpoint values, and the subscript of the first element of the array is 0.
***/
<span><br>$a</span>=<span>array</span>('3','8','1','4','11','7'<span>); </span><span>print_r</span>(<span>$a</span><span>); </span><span>$len</span> = <span>count</span>(<span>$a</span><span>); </span><span>//</span><span>从小到大</span><span>for</span>(<span>$i</span>=1;<span>$i</span><<span>$len</span>;<span>$i</span>++<span>) { </span><span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>>=<span>$i</span>;<span>$j</span>--<span>) </span><span>if</span>(<span>$a</span>[<span>$j</span>]<<span>$a</span>[<span>$j</span>-1<span>]) {</span><span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了<span>$x</span>=<span>$a</span>[<span>$j</span><span>]; </span><span>$a</span>[<span>$j</span>]=<span>$a</span>[<span>$j</span>-1<span>]; </span><span>$a</span>[<span>$j</span>-1]=<span>$x</span><span>; } } </span><span>print_r</span>(<span>$a</span><span>); </span><span>//</span><span>另一种方法 从小到大</span><span>$b</span>=<span>array</span>('4','3','8','9','2','1'<span>); </span><span>$len</span>=<span>count</span>(<span>$b</span><span>); </span><span>for</span>(<span>$k</span>=1;<span>$k</span><<span>$len</span>;<span>$k</span>++<span>) { </span><span>for</span>(<span>$j</span>=<span>$len</span>-1,<span>$i</span>=0;<span>$i</span><<span>$len</span>-<span>$k</span>;<span>$i</span>++,<span>$j</span>--<span>) </span><span>if</span>(<span>$b</span>[<span>$j</span>]<<span>$b</span>[<span>$j</span>-1<span>]){ </span><span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了<span>$tmp</span>=<span>$b</span>[<span>$j</span><span>]; </span><span>$b</span>[<span>$j</span>]=<span>$b</span>[<span>$j</span>-1<span>]; </span><span>$b</span>[<span>$j</span>-1]=<span>$tmp</span><span>; } </span><span>print_r</span>(<span>$b</span><span>); </span><span>echo</span> " "<span>; } </span><span>//</span><span>下面的这个执行效率更高</span><span>function</span> maopao(<span>$arr</span><span>) { </span><span>$len</span> = <span>count</span>(<span>$arr</span><span>); </span><span>for</span>(<span>$i</span>=1; <span>$i</span><<span>$len</span>; <span>$i</span>++)<span>//</span><span>最多做n-1趟排序</span><span> { </span><span>$flag</span> = <span>false</span>; <span>//</span><span>本趟排序开始前,交换标志应为假</span><span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>>=<span>$i</span>;<span>$j</span>--<span>) { </span><span>if</span>(<span>$arr</span>[<span>$j</span>]<<span>$arr</span>[<span>$j</span>-1])<span>//</span><span>交换记录</span> {<span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($arr[$j]>$arr[$j-1])就可以了<span>$x</span>=<span>$arr</span>[<span>$j</span><span>]; </span><span>$arr</span>[<span>$j</span>]=<span>$arr</span>[<span>$j</span>-1<span>]; </span><span>$arr</span>[<span>$j</span>-1]=<span>$x</span><span>; </span><span>$flag</span> = <span>true</span>;<span>//</span><span>发生了交换,故将交换标志置为真</span><span> } } </span><span>if</span>(! <span>$flag</span>)<span>//</span><span>本趟排序未发生交换,提前终止算法</span><span>return</span><span>$arr</span><span>; } } </span><span>$shuz</span> = <span>array</span>('2','4','1','8','5'<span>); </span><span>$bb</span> = maopao(<span>$shuz</span><span>); </span><span>print_r</span>(<span>$bb</span><span>); </span><span>//</span><span> 快速排序</span><span>function</span> kuaisu(<span>$arr</span><span>){ </span><span>$len</span> = <span>count</span>(<span>$arr</span><span>); </span><span>if</span>(<span>$len</span> <= 1<span>){ </span><span>return</span><span>$arr</span><span>; } </span><span>$key</span> = <span>$arr</span>[0<span>]; </span><span>$left_arr</span> = <span>array</span><span>(); </span><span>$right_arr</span> = <span>array</span><span>(); </span><span>for</span>(<span>$i</span>=1; <span>$i</span><<span>$len</span>;<span>$i</span>++<span>){ </span><span>if</span>(<span>$arr</span>[<span>$i</span>] <= <span>$key</span><span>){ </span><span>$left_arr</span>[] = <span>$arr</span>[<span>$i</span><span>]; }</span><span>else</span><span>{ </span><span>$right_arr</span>[] = <span>$arr</span>[<span>$i</span><span>]; } } </span><span>$left_arr</span> = kuaisu(<span>$left_arr</span><span>); </span><span>$right_arr</span> = kuaisu(<span>$right_arr</span><span>); </span><span>return</span><span>array_merge</span>(<span>$left_arr</span>, <span>array</span>(<span>$key</span>), <span>$right_arr</span><span>); } </span><span>$arr</span> = <span>array</span>(23,98,54,2,9,62,34<span>); </span><span>print_r</span>(kuaisu(<span>$arr</span>));
The above introduces PHP bubble sort and quick sort, including insertion sort. 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

AI Hentai Generator
Generate AI Hentai for free.

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



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

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

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

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

Interpretation of CSS positioning properties: position and top/left/right/bottom In front-end development, CSS positioning properties are very important. With the positioning attribute, we can control the position of the element on the page. The most commonly used positioning attribute is position, whose values can be static, relative, absolute, and fixed. In addition to these basic positioning attributes, we can also use top, left, r

In PHP programming, array is a frequently used data type. There are also quite a few array operation functions, including the array_change_key_case() function. This function can convert the case of key names in the array to facilitate our data processing. This article will introduce how to use the array_change_key_case() function in PHP. 1. Function syntax and parameters array_change_ke

The array module in Python is a predefined array, so it takes up much less space in memory than a standard list, and can also perform fast element-level operations such as adding, deleting, indexing, and slicing. In addition, all elements in the array are of the same type, so you can use the efficient numerical operation functions provided by the array, such as calculating the average, maximum, and minimum values. In addition, the array module also supports writing and reading array objects directly into binary files, which makes it more efficient when processing large amounts of numerical data. Therefore, if you need to process a large amount of homogeneous data, you may consider using Python's array module to optimize the execution efficiency of your code. To use the array module, you first need to

The printscreen key is on the arrow keys of the keyboard device, with the words "prtsc sysrq" on it, and is located to the right of f12. If there is no button with the word "prtsc sysrq", you can find "fn" and "insert(prt sc)", click "fn" first, and then click "insert(PRT sc)" to realize the printscreen screenshot function.
