Skip to content

Commit

Permalink
冒泡排序
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexchent committed Jul 4, 2021
1 parent f167763 commit 3f4af66
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions codehero/perg_match.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php/** * Created by PhpStorm. * User: chentao * Date: 2021/7/4 * Time: 9:18 PM */$str = 'src="http:www.youzu.com/1.png" width="100" width="100"';//echo preg_match("/src=*(.*)*/", $str);
Expand Down
1 change: 1 addition & 0 deletions suanfa/Base/bubble_sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php/** * Created by PhpStorm. * User: chentao * Date: 2021/7/4 * Time: 10:55 PM *///冒泡排序的思想是:从第一个数开始,跟后面的数比较,如果比后面的数大则交换位置,那么更最后一个数比完,最大的数就排到了最后面//接着再从头开始,比较剩下的n-1个数//如此往复,直到最后一个数//时间复杂度 O(n^2)function bubble_sort($arr){ $len = count($arr); if ($len < 2) return $arr; for($i=1; $i<$len; $i++) { for ($j=0; $j<$len-1;$j++) { if ($arr[$j] > $arr[$j+1]) { $tmp = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $tmp; } } } return $arr;}$sort = bubble_sort([2,6,19,4,5,8,3]);var_dump($sort);
Expand Down
1 change: 1 addition & 0 deletions suanfa/Base/quick_sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php/** * Created by PhpStorm. * User: chentao * Date: 2021/7/4 * Time: 10:42 PM *///快速排序的思路,就是选中一个基准数,比基准数大的放在右区间,否则放在左区间,这样基准数的位置就确定了,//之后就是对区间重复上诉操作,当所有的区间只有一个数的时候就排好序了//快速排序的时间平均时间复杂度是 O(nlog2n) 最坏时间复杂度是O(n^2)function quick_sort($arr) { if (count($arr) < 2) return $arr; $base = $arr[0]; $right = []; $left = []; unset($arr[0]); foreach ($arr as $value) { if ($value >= $base) { $right[] = $value; } else { $left[] = $value; } } if (count($left)) $left = quick_sort($left); if (count($right)) $right = quick_sort($right); return array_merge($left, [$base], $right);}$sort = quick_sort([2,6,19,4,5,8,3]);var_dump($sort);
Expand Down

0 comments on commit 3f4af66

Please sign in to comment.