Skip to content

Commit

Permalink
添加PHP代码实现及测试文件 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilon authored and hustcc committed Apr 16, 2018
1 parent 6954cf8 commit 920ab60
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 1.bubbleSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,22 @@ public class BubbleSort implements IArraySort {
}
}
```

## 9. PHP 代码实现

```php
function bubbleSort($arr)
{
$len = count($arr);
for ($i = 0; $i < $len - 1; $i++) {
for ($j = 0; $j < $len - 1 - $i; $j++) {
if ($arr[$j] > $arr[$j+1]) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];
$arr[$j+1] = $tmp;
}
}
}
return $arr;
}
```
37 changes: 37 additions & 0 deletions 10.radixSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,40 @@ public class RadixSort implements IArraySort {
}
}
```

## 5. PHP 代码实现

```php
function radixSort($arr, $maxDigit = null)
{
if ($maxDigit === null) {
$maxDigit = max($arr);
}
$counter = [];
for ($i = 0; $i < $maxDigit; $i++) {
for ($j = 0; $j < count($arr); $j++) {
preg_match_all('/\d/', (string) $arr[$j], $matches);
$numArr = $matches[0];
$lenTmp = count($numArr);
$bucket = array_key_exists($lenTmp - $i - 1, $numArr)
? intval($numArr[$lenTmp - $i - 1])
: 0;
if (!array_key_exists($bucket, $counter)) {
$counter[$bucket] = [];
}
$counter[$bucket][] = $arr[$j];
}
$pos = 0;
for ($j = 0; $j < count($counter); $j++) {
$value = null;
if ($counter[$j] !== null) {
while (($value = array_shift($counter[$j])) !== null) {
$arr[$pos++] = $value;
}
}
}
}

return $arr;
}
```
21 changes: 21 additions & 0 deletions 2.selectionSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,24 @@ public class SelectionSort implements IArraySort {
}
}
```

## 7. PHP 代码实现

```php
function selectionSort($arr)
{
$len = count($arr);
for ($i = 0; $i < $len - 1; $i++) {
$minIndex = $i;
for ($j = $i + 1; $j < $len; $j++) {
if ($arr[$j] < $arr[$minIndex]) {
$minIndex = $j;
}
}
$temp = $arr[$i];
$arr[$i] = $arr[$minIndex];
$arr[$minIndex] = $temp;
}
return $arr;
}
```
19 changes: 19 additions & 0 deletions 3.insertionSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,22 @@ public class InsertSort implements IArraySort {
}
}
```

## 7. PHP 代码实现

```php
function insertionSort($arr)
{
$len = count($arr);
for ($i = 1; $i < $len; $i++) {
$preIndex = $i - 1;
$current = $arr[$i];
while($preIndex >= 0 && $arr[$preIndex] > $current) {
$arr[$preIndex+1] = $arr[$preIndex];
$preIndex--;
}
$arr[$preIndex+1] = $current;
}
return $arr;
}
```
24 changes: 24 additions & 0 deletions 4.shellSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,27 @@ public class ShellSort implements IArraySort {
}
}
```

## 6. PHP 代码实现

```php
function shellSort($arr)
{
$len = count($arr);
$temp = 0;
$gap = 1;
while($gap < $len / 3) {
$gap = $gap * 3 + 1;
}
for ($gap; $gap > 0; $gap = floor($gap / 3)) {
for ($i = $gap; $i < $len; $i++) {
$temp = $arr[$i];
for ($j = $i - $gap; $j >= 0 && $arr[$j] > $temp; $j -= $gap) {
$arr[$j+$gap] = $arr[$j];
}
$arr[$j+$gap] = $temp;
}
}
return $arr;
}
```
37 changes: 37 additions & 0 deletions 5.mergeSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,40 @@ public class MergeSort implements IArraySort {

}
```

## 8. PHP 代码实现

```php
function mergeSort($arr)
{
$len = count($arr);
if ($len < 2) {
return $arr;
}
$middle = floor($len / 2);
$left = array_slice($arr, 0, $middle);
$right = array_slice($arr, $middle);
return merge(mergeSort($left), mergeSort($right));
}

function merge($left, $right)
{
$result = [];

while (count($left) > 0 && count($right) > 0) {
if ($left[0] <= $right[0]) {
$result[] = array_shift($left);
} else {
$result[] = array_shift($right);
}
}

while (count($left))
$result[] = array_shift($left);

while (count($right))
$result[] = array_shift($right);

return $result;
}
```
25 changes: 25 additions & 0 deletions 6.quickSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,28 @@ public class QuickSort implements IArraySort {
}
```

## 8. PHP 代码实现

```php
function quickSort($arr)
{
if (count($arr) <= 1)
return $arr;
$middle = $arr[0];
$leftArray = array();
$rightArray = array();

for ($i = 1; $i < count($arr); $i++) {
if ($arr[$i] > $middle)
$rightArray[] = $arr[$i];
else
$leftArray[] = $arr[$i];
}
$leftArray = quickSort($leftArray);
$leftArray[] = $middle;

$rightArray = quickSort($rightArray);
return array_merge($leftArray, $rightArray);
}
```
52 changes: 52 additions & 0 deletions 7.heapSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,55 @@ public class HeapSort implements IArraySort {

}
```

## 7. PHP 代码实现

```php
function buildMaxHeap(&$arr)
{
global $len;
for ($i = floor($len/2); $i >= 0; $i--) {
heapify($arr, $i);
}
}

function heapify(&$arr, $i)
{
global $len;
$left = 2 * $i + 1;
$right = 2 * $i + 2;
$largest = $i;

if ($left < $len && $arr[$left] > $arr[$largest]) {
$largest = $left;
}

if ($right < $len && $arr[$right] > $arr[$largest]) {
$largest = $right;
}

if ($largest != $i) {
swap($arr, $i, $largest);
heapify($arr, $largest);
}
}

function swap(&$arr, $i, $j)
{
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}

function heapSort($arr) {
global $len;
$len = count($arr);
buildMaxHeap($arr);
for ($i = count($arr) - 1; $i > 0; $i--) {
swap($arr, 0, $i);
$len--;
heapify($arr, 0);
}
return $arr;
}
```
29 changes: 29 additions & 0 deletions 8.countingSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,32 @@ public class CountingSort implements IArraySort {

}
```

## 6. PHP 代码实现

```php
function countingSort($arr, $maxValue = null)
{
if ($maxValue === null) {
$maxValue = max($arr);
}
for ($m = 0; $m < $maxValue + 1; $m++) {
$bucket[] = null;
}

$arrLen = count($arr);
for ($i = 0; $i < $arrLen; $i++) {
if (!array_key_exists($arr[$i], $bucket)) {
$bucket[$arr[$i]] = 0;
}
$bucket[$arr[$i]]++;
}

$sortedIndex = 0;
foreach ($bucket as $key => $len) {
if ($len !== null) $arr[$sortedIndex++] = $key;
}

return $arr;
}
```
42 changes: 42 additions & 0 deletions 9.bucketSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,45 @@ public class BucketSort implements IArraySort {

}
```

## 5. PHP 代码实现

```php
function bucketSort($arr, $bucketSize = 5)
{
if (count($arr) === 0) {
return $arr;
}

$minValue = $arr[0];
$maxValue = $arr[0];
for ($i = 1; $i < count($arr); $i++) {
if ($arr[$i] < $minValue) {
$minValue = $arr[$i];
} else if ($arr[$i] > $maxValue) {
$maxValue = $arr[$i];
}
}

$bucketCount = floor(($maxValue - $minValue) / $bucketSize) + 1;
$buckets = array();
for ($i = 0; $i < count($buckets); $i++) {
$buckets[$i] = [];
}

for ($i = 0; $i < count($arr); $i++) {
$buckets[floor(($arr[$i] - $minValue) / $bucketSize)][] = $arr[$i];
}

$arr = array();
for ($i = 0; $i < count($buckets); $i++) {
$bucketTmp = $buckets[$i];
sort($bucketTmp);
for ($j = 0; $j < count($bucketTmp); $j++) {
$arr[] = $bucketTmp[$j];
}
}

return $arr;
}
```
Loading

0 comments on commit 920ab60

Please sign in to comment.