Collection of PHP classic algorithms

高洛峰
Release: 2023-03-03 21:22:01
Original
1233 people have browsed it

The examples in this article summarize PHP classic algorithms. Share it with everyone for your reference, the details are as follows:

1. First, let’s draw a rhombus for fun. Many people have drawn it in books when they were learning C. Let’s draw it in PHP and draw half of it.

Idea: For once for as many lines, and then for once for spaces and asterisks inside.

'; }
Copy after login

2. Bubble sorting, the basic algorithm in C, sorts a set of numbers from small to large.

Thinking: This question is ranked from smallest to largest. The first round is the smallest, the second round is the second smallest, the third round is the third smallest, and so on...

$arr[$j]){//从小到大 $p = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j]= $p; } } } var_dump($arr);
Copy after login

3. Yang Hui Triangle, using PHP Write.

Idea: The first and last digits of each row are 1, and there is no change. The middle is the sum of the first digit and the left row. This algorithm is saved in a two-dimensional array. There is another algorithm that uses a Dimensional arrays can also be implemented, and the output is line by line. If you are interested, you can write and play with it.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

'; }
Copy after login

4. In a set of numbers, if you want to insert a number, press Insert in the original order and maintain the original sorting method.

Idea: Find the position that is larger than the number to be inserted, replace it, and then move the following number back by one.

= $in){ $t1= $arr[$i]; $arr[$i] = $in; //把后面的数据后移一位 for($j=$i+1; $j<$n+1; $j++) { $t2 = $arr[$j]; $arr[$j] = $t1; $t1 = $t2; } //打印 print_r($arr); die; } }
Copy after login

5. Sort a set of numbers (quick sort algorithm).

Idea: Divide into two parts through one sorting, then recursively sort the two parts, and finally merge.


        
Copy after login

6. Find the element you need in an array (binary search algorithm).

Idea: Use a certain value in the array as the boundary, and then search recursively until the end.


        
Copy after login

7. Merge multiple arrays without array_merge(). The question comes from the forum.

Idea: Traverse each array and re-form a new array. Question How many cows will there be in n years?

'; $a = array_merge(range(1,4),range(1,4),range(1,4)); print_r($a);
Copy after login

==================== Other algorithms====================== ====

bubble sort (bubble sort) — O(n2)

=4 && $j<15) {$num++;t($n-$j);} if($j==20){$num--;} } return $num; } //test echo t(8);
Copy after login

insertion sort (insertion sort) — O(n2)

$data = array(3,5,9,32,2,1,2,1,8,5); dump($data); BubbleSort($data); dump($data); function BubbleSort(& $arr) { $limit = count($arr); for($i=1; $i<$limit; $i++) { for($p=$limit-1; $p>=$i; $p--) { if($arr[$p-1] > $arr[$p]) { $temp = $arr[$p-1]; $arr[$p-1] = $arr[$p]; $arr[$p] = $temp; } } } } function dump( $d ) { echo '
'; print_r($d); echo '
'; }
Copy after login

Hill sort (shell sort) — O(n log n)

$data = array(6,13,21,99,18,2,25,33,19,84); $nums = count($data)-1; dump( $data ); InsertionSort($data,$nums); dump( $data ); function InsertionSort(& $arr,$n ) { for( $i=1; $i<=$n; $i++ ) { $tmp = $arr[$i]; for( $j = $i; $j>0 && $arr[$j-1]>$tmp; $j-- ) { $arr[$j] = $arr[$j-1]; } $arr[$j] = $tmp; } } function dump( $d ) { echo '
';print_r($d);echo '
'; }
Copy after login

Quicksort (quicksort) — O(n log n)

$data = array(6,13,21,99,18,2,25,33,19,84); $nums = count($data); dump( $data ); ShellSort($data,$nums); dump( $data ); function ShellSort(& $arr,$n ) { for( $increment = intval($n/2); $increment > 0; $increment = intval($increment/2) ) { for( $i=$increment; $i<$n; $i++ ) { $tmp = $arr[$i]; for( $j = $i; $j>= $increment; $j -= $increment ) if( $tmp < $arr[ $j-$increment ] ) $arr[$j] = $arr[$j-$increment]; else break; $arr[$j] = $tmp; } } } function dump( $d ) { echo '
';print_r($d);echo '
'; }
Copy after login

================== ================================

Bubble sort: exchange values in pairs, with the smallest value on the left. Like the lightest bubbles are on top. Swap 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 values form an An unordered interval, and the value of each element in the ordered interval is smaller than that of the unordered interval.

Quick sort: base number, left and right arrays, recursive call, merge.

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 If it is smaller than the rightmost element of 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 the previous element will be compared again until the previous element is smaller than the element to be inserted. , repeat the above steps.

Pay attention to the processing of the endpoint value of the interval, and the subscript of the first element of the array is 0.

$data = array(6,13,21,99,18,2,25,33,19,84); dump($data); quicks($data,0,count($data)-1); dump($data); function dump( $data){ echo '
';print_r($data);echo '
'; } function QuickSort(& $arr,$left,$right) { $l = $left; $r = $right; $pivot = intval(($r+$l)/2); $p = $arr[$pivot]; do { while(($arr[$l] < $p) && ($l < $right)) $l++; while(($arr[$r] > $p) && ($r > $left)) $r--; if($l <= $r) { $temp = $arr[$l]; $arr[$l] = $arr[$r]; $arr[$r] = $temp; $l++; $r--; } } while($l <= $r); if($left < $r) QuickSort(&$arr,$left,$r); if($l < $right) QuickSort(&$arr,$l,$right); }
Copy after login

======================== =================

= $i; $j--) //[0,i-1] [i,n-1] { if ($array[$j] > $array[$j + 1]) { $temp = $array[$j]; $array[$j] = $array[$j + 1]; $array [$j + 1] = $temp; } } } return $array; } $array = array (3,6,1,5,9,0,4,6,11); print_r (bubblesort ($array)); echo '
'; function quicksort ($array) { $n = count ($array); if ($n <= 1) { return $array; } $key = $array['0']; $array_r = array (); $array_l = array (); for ($i = 1; $i < $n; $i++) { if ($array[$i] > $key) { $array_r[] = $array[$i]; } else { $array_l[] = $array[$i]; } } $array_r = quicksort ($array_r); $array_l = quicksort ($array_l); $array = array_merge ($array_l, array($key), $array_r); return $array; } print_r (quicksort ($array)); echo '
'; function insertsort ($array) { $n = count ($array); for ($i = 1; $i < $n; $i++) //[0,i-1] [i,n] { $j = $i - 1; $temp = $array[$i]; while ($array[$j] > $temp) { $array[$j + 1] = $array[$j]; $array[$j] = $temp; $j--; } } return $array; } print_r (insertsort ($array)); ?>
Copy after login

 $tmp){ $arr[$j+1] = $arr[$j]; $arr[$j] = $tmp; $j--; } } return $arr; } /* 【选择排序(一维数组)】 【基 本思想】:每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 【示例】: [初 始关键字] [49 38 65 97 76 13 27 49] 第一趟排序后 13 [38 65 97 76 49 27 49] 第 二趟排序后 13 27 [65 97 76 49 38 49] 第三趟排序后 13 27 38 [97 76 49 65 49] 第 四趟排序后 13 27 38 49 [49 97 65 76] 第五趟排序后 13 27 38 49 49 [97 97 76] 第 六趟排序后 13 27 38 49 49 76 [76 97] 第七趟排序后 13 27 38 49 49 76 76 [ 97] 最 后排序结果 13 27 38 49 49 76 76 97 */ function select_sort($arr){ $count = count($arr); for($i=0; $i<$count; $i++){ $k = $i; for($j=$i+1; $j<$count; $j++){ if ($arr[$k] > $arr[$j]) $k = $j; } if($k != $i){ $tmp = $arr[$i]; $arr[$i] = $arr[$k]; $arr[$k] = $tmp; } } return $arr; } /* 【冒泡排序(一维数组) 】 【基本思想】:两两比较待排序数据元素的大小,发现两个数据元素的次序 相反时即进行交换,直到没有反序的数据元素为止。 【排序过程】:设想被排序的数组R[1..N]垂直竖立,将每个数据元素看作有重量的气泡,根据 轻气泡不能在重气泡之下的原则, 从下往上扫描数组R,凡扫描到违反本原则的轻气泡,就使其向上"漂浮",如此反复进行,直至最后任何两个气泡都是 轻者在上,重者在下为止。 【示例】: 49 13 13 13 13 13 13 13 38 49 27 27 27 27 27 27 65 38 49 38 38 38 38 38 97 65 38 49 49 49 49 49 76 97 65 49 49 49 49 49 13 76 97 65 65 65 65 65 27 27 76 97 76 76 76 76 49 49 49 76 97 97 97 97 */ function bubble_sort($array){ $count = count($array); if ($count <= 0) return false; for($i=0; $i<$count; $i++){ for($j=$count-1; $j>$i; $j--){ if ($array[$j] < $array[$j-1]){ $tmp = $array[$j]; $array[$j] = $array[$j-1]; $array[$j-1] = $tmp; } } } return $array; } /* 【快速排序(一 维数组)】 【基本思想】:在当前无序区R[1..H]中任取一个数据元素作为比较的"基准"(不妨记为X), 用此基准将当前无序区划分为 左右两个较小的无序区:R[1..I-1]和R[I 1..H],且左边的无序子区中数据元素均小于等于基准元素, 右边的无序子区中数据元素均大 于等于基准元素,而基准X则位于最终排序的位置上,即R[1..I-1]≤X.Key≤R[I 1..H](1≤I≤H), 当R[1..I-1] 和R[I 1..H]均非空时,分别对它们进行上述的划分过程,直至所有无序子区中的数据元素均已排序为止。 【示例】: 初始关键字 [49 38 65 97 76 13 27 49] 第一次交换后 [27 38 65 97 76 13 49 49] 第二次交换后 [27 38 49 97 76 13 65 49] J向左扫描,位置不变,第三次交换后 [27 38 13 97 76 49 65 49] I向右扫描,位置不变,第四次交换后 [27 38 13 49 76 97 65 49] J向左扫描 [27 38 13 49 76 97 65 49] (一次划分过程) 初始关键字 [49 38 65 97 76 13 27 49] 一趟排序之后 [27 38 13] 49 [76 97 65 49] 二趟排序之后 [13] 27 [38] 49 [49 65]76 [97] 三趟排序之后 13 27 38 49 49 [65]76 97 最后的排序结果 13 27 38 49 49 65 76 97 各趟排序之后的状态 */ function quick_sort($array){ if (count($array) <= 1) return $array; $key = $array[0]; $left_arr = array(); $right_arr = array(); for ($i=1; $i'; } /* 几种排序算法的比较和选择 1. 选取排序方法需要考虑的因素: (1) 待排序的元素数目n; (2) 元素本身信息量的大小; (3) 关键字的结构及其分布情况; (4) 语言工具的条件,辅助空间的大小等。 2. 小结: (1) 若n较小(n <= 50),则可以采用直接插入排序或直接选择排序。由于直接插入排序所需的记录移动操作较直接选择排序多,因而当记录本身信息量较大时,用直接选择排序较 好。 (2) 若文件的初始状态已按关键字基本有序,则选用直接插入或冒泡排序为宜。 (3) 若n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序。 快速排序是目前基于比较的内部排序法中被认为是最好的方法。 (4) 在基于比较排序方法中,每次比较两个关键字的大小之后,仅仅出现两种可能的转移,因此可以用一棵二叉树来描述比较判定过程,由此可以证明:当文件的n个关 键字随机分布时,任何借助于"比较"的排序算法,至少需要O(nlog2n)的时间。 (5) 当记录本身信息量较大时,为避免耗费大量时间移动记录,可以用链表作为存储结构。 */ /*排序测试*/ $a = array('12','4','16','8','13','20','5','32'); echo 'The result of insert sort:'; $insert_a = insert_sort($a); display_arr($insert_a); echo 'The result of select sort:'; $select_a = select_sort($a); display_arr($select_a); echo 'The result of bubble sort:'; $bubble_a = bubble_sort($a); display_arr($bubble_a); echo 'The result of bubble sort:'; $quick_a = quick_sort($a); display_arr($quick_a); ?>
Copy after login

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP classic algorithms, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!