Skip to content

Commit d6c94a0

Browse files
committed
feat: new arithmetic 🔨
1 parent 6745a11 commit d6c94a0

File tree

10 files changed

+122
-26
lines changed

10 files changed

+122
-26
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
│ │ ├── BubbleSort.php 冒泡排序
2121
│ │ ├── QuickSort.php 快速排序
2222
│ │ ├── ShellSort.php 希尔排序
23-
│ │ └── MergeSort.php 归并排序
23+
│ │ ├── MergeSort.php 归并排序
24+
│ │ ├── InsertSort.php 插入排序
25+
│ │ └── SelectSort.php 选择排序
2426
│ │
2527
│ ├── Query 查询篇
2628
│ │ ├── BinaryQuery.php 二分查找

package/Other/BigSmallReplace.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
* @author Pu ShaoWei <[email protected]>
77
* @date 2017/8/12
88
* @license Mozilla
9-
*/
10-
11-
/**
9+
*
1210
* segmentFault: https://segmentfault.com/q/1010000010627229
1311
*
1412
* @language php

package/Query/BinaryQuery.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
* 若比中间值大则将首值替换为中间位置下一个位置,继续第一步的操作;
1212
* 若比中间值小,则将尾值替换为中间位置上一个位置,继续第一步操作
1313
* 重复第二步操作直至找出目标数字
14-
*/
15-
16-
/**
14+
*
1715
* 非递归版 二分查找
1816
* @param array $container
1917
* @param $search

package/Query/QulickQuery.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
* 思路分析:数组中间的值floor((low+top)/2) 
1111
* -------------------------------------------------------------
1212
* 重复第二步操作直至找出目标数字
13-
*/
14-
15-
/**
13+
*
1614
* @param $array
1715
* @param $k
1816
* @param int $low

package/Sort/BubbleSort.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
* 由底至上的把较少的气泡逐步地向上升,这样经过遍历一次后最小的气泡就会被上升到顶(下标为0)
1212
* 然后再从底至上地这样升,循环直至十个气泡大小有序。
1313
* 在冒泡排序中,最重要的思想是两两比较,将两者较少的升上去
14-
*/
15-
16-
/**
14+
*
1715
* @param array $container
1816
* @return array
1917
*/

package/Sort/InsertSort.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* @example 插入排序
5+
* @author ShaoWei Pu <[email protected]>
6+
* @date 2017/6/17
7+
* @license Mozilla
8+
* -------------------------------------------------------------
9+
* 思路分析:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
10+
* -------------------------------------------------------------
11+
*
12+
* 算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。
13+
* 插入算法把要排序的数组分成两部分:第一部分包含了这个数组的所有元素,
14+
* 但将最后一个元素除外(让数组多一个空间才有插入的位置),而第二部分就只包含这一个元素(即待插入元素)。
15+
* 在第一部分排序完成后,再将这个最后元素插入到已排好序的第一部分中。
16+
*
17+
* @param $container
18+
* @return mixed
19+
*/
20+
21+
function InsertSort(array $container)
22+
{
23+
$count = count($container);
24+
for ($i = 1; $i < $count; $i++){
25+
$temp = $container[$i];
26+
$j = $i - 1;
27+
// Init
28+
while ($container[$j] > $temp){
29+
$container[$j+1] = $container[$j];
30+
$container[$j] = $temp;
31+
$j--;
32+
if ($j < 0) break;
33+
}
34+
}
35+
return $container;
36+
}
37+
var_dump(InsertSort([3, 12, 42, 1, 24, 5, 346, 7]));
38+
39+
/*
40+
array (size=8)
41+
0 => int 1
42+
1 => int 3
43+
2 => int 5
44+
3 => int 7
45+
4 => int 12
46+
5 => int 24
47+
6 => int 42
48+
7 => int 346
49+
*/

package/Sort/MergeSort.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ public function mSort(&$arr, $left, $right)
6060
*/
6161
public function mergeArray(&$arr, $left, $center, $right)
6262
{
63-
echo '| '.$left.' - '.$center.' - '.$right.' - '.implode(',',$arr).PHP_EOL;
63+
echo '| ' . $left . ' - ' . $center . ' - ' . $right . ' - ' . implode(',', $arr) . PHP_EOL;
6464
//设置两个起始位置标记
65-
$a_i = $left;
66-
$b_i = $center + 1;
65+
$a_i = $left;
66+
$b_i = $center + 1;
6767
$temp = [];
6868

69-
while ($a_i <= $center && $b_i <= $right){
69+
while ($a_i <= $center && $b_i <= $right) {
7070
//当数组A和数组B都没有越界时
7171
if ($arr[ $a_i ] < $arr[ $b_i ]) {
7272
$temp[] = $arr[ $a_i++ ];
@@ -75,16 +75,16 @@ public function mergeArray(&$arr, $left, $center, $right)
7575
}
7676
}
7777
//判断 数组A内的元素是否都用完了,没有的话将其全部插入到C数组内:
78-
while ($a_i <= $center){
78+
while ($a_i <= $center) {
7979
$temp[] = $arr[ $a_i++ ];
8080
}
8181
//判断 数组B内的元素是否都用完了,没有的话将其全部插入到C数组内:
82-
while ($b_i <= $right){
82+
while ($b_i <= $right) {
8383
$temp[] = $arr[ $b_i++ ];
8484
}
8585

8686
//将$arrC内排序好的部分,写入到$arr内:
87-
for ($i = 0, $len = count($temp); $i < $len; $i++){
87+
for ($i = 0, $len = count($temp); $i < $len; $i++) {
8888
$arr[ $left + $i ] = $temp[ $i ];
8989
}
9090
}

package/Sort/QuickSort.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
* 重新排序数列,所有元素比基准值小的摆放在基准前面
1111
* 所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。
1212
* 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序
13-
*/
14-
15-
/**
13+
*
1614
* @param array $container
1715
* @return array|void
1816
*/

package/Sort/SelectSort.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* @example 选择排序
5+
* @author ShaoWei Pu <[email protected]>
6+
* @date 2017/6/17
7+
* @license Mozilla
8+
* -------------------------------------------------------------
9+
* 思路分析:选择排序是不稳定的排序方法
10+
* -------------------------------------------------------------
11+
* 它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。
12+
* 选择排序是不稳定的排序方法(比如序列[5, 5, 3]第一次就将第一个[5]与[3]交换,导致第一个5挪动到第二个5后面)。
13+
*
14+
* @param $container
15+
* @return mixed
16+
*/
17+
function SelectSort(array $container)
18+
{
19+
$count = count($container);
20+
for ($i = 0; $i < $count; $i++){
21+
$k = $i;
22+
for ($j = $i + 1; $j < $count; $j++){
23+
if($container[$j] < $container[$k]){
24+
$k = $j;
25+
}
26+
}
27+
if($k != $i){
28+
$temp = $container[$i];
29+
$container[$i] = $container[$k];
30+
$container[$k] = $temp;
31+
}
32+
}
33+
return $container;
34+
}
35+
36+
var_dump(SelectSort([3, 12, 42, 1, 24, 5, 346, 7]));
37+
38+
/*
39+
array(8) {
40+
[0] =>
41+
int(1)
42+
[1] =>
43+
int(3)
44+
[2] =>
45+
int(5)
46+
[3] =>
47+
int(7)
48+
[4] =>
49+
int(12)
50+
[5] =>
51+
int(24)
52+
[6] =>
53+
int(42)
54+
[7] =>
55+
int(346)
56+
}
57+
*/

package/Sort/ShellSort.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
* -------------------------------------------------------------
1010
* 希尔排序中一个常数因子n,原数组被分成各个小组,每个小组由h个元素组成,很可能会有多余的元素。
1111
* 当然每次循环的时候,h也是递减的(h=h/n)。第一次循环就是从下标为h开始。希尔排序的一个思想就是,分成小组去排序。
12-
*/
13-
14-
/**
12+
*
1513
* @param array $container
1614
* @return array
1715
*/

0 commit comments

Comments
 (0)