From 920ab6052d97d10e7b8215f49f49b45f237cab38 Mon Sep 17 00:00:00 2001 From: Weilong Wang Date: Mon, 16 Apr 2018 09:57:55 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0PHP=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=8F=8A=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= =?UTF-8?q?=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.bubbleSort.md | 19 +++ 10.radixSort.md | 37 ++++++ 2.selectionSort.md | 21 +++ 3.insertionSort.md | 19 +++ 4.shellSort.md | 24 ++++ 5.mergeSort.md | 37 ++++++ 6.quickSort.md | 25 ++++ 7.heapSort.md | 52 ++++++++ 8.countingSort.md | 29 +++++ 9.bucketSort.md | 42 ++++++ src/phpSortTest.php | 304 ++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 609 insertions(+) create mode 100644 src/phpSortTest.php diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 621314f..78c3b83 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -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; +} +``` diff --git a/10.radixSort.md b/10.radixSort.md index 39b0a64..0bcd341 100644 --- a/10.radixSort.md +++ b/10.radixSort.md @@ -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; +} +``` \ No newline at end of file diff --git a/2.selectionSort.md b/2.selectionSort.md index ae7350f..103830e 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -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; +} +``` diff --git a/3.insertionSort.md b/3.insertionSort.md index 71beec9..c828cb3 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -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; +} +``` diff --git a/4.shellSort.md b/4.shellSort.md index c96d8b7..187ac0e 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -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; +} +``` diff --git a/5.mergeSort.md b/5.mergeSort.md index 8394ab5..bf0de32 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -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; +} +``` \ No newline at end of file diff --git a/6.quickSort.md b/6.quickSort.md index 497f973..28ad477 100644 --- a/6.quickSort.md +++ b/6.quickSort.md @@ -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); +} +``` diff --git a/7.heapSort.md b/7.heapSort.md index 43d522c..35b24ec 100644 --- a/7.heapSort.md +++ b/7.heapSort.md @@ -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; +} +``` \ No newline at end of file diff --git a/8.countingSort.md b/8.countingSort.md index b0f17e5..efabf9c 100644 --- a/8.countingSort.md +++ b/8.countingSort.md @@ -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; +} +``` \ No newline at end of file diff --git a/9.bucketSort.md b/9.bucketSort.md index e2e4aa6..bd76a63 100644 --- a/9.bucketSort.md +++ b/9.bucketSort.md @@ -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; +} +``` \ No newline at end of file diff --git a/src/phpSortTest.php b/src/phpSortTest.php new file mode 100644 index 0000000..4fc59d5 --- /dev/null +++ b/src/phpSortTest.php @@ -0,0 +1,304 @@ + + * + * 主要参考了 JS 的写法及网上的一些写法。 + * + * Require: php -v >= 5.4 + * Test: php phpSortTest.php + */ + +function sortTest($func, $total = 5000) +{ + global $arr; + if (empty($arr)) { + $arr = range(1, $total); + echo "Verify sort md5: ", substr(md5(json_encode($arr)), 0, 8), "\r\n"; + shuffle($arr); + } + list($m1, $n1) = explode(' ', microtime()); + $res = $func($arr); + list($m2, $n2) = explode(' ', microtime()); + $time = round(($m2 - $m1) + ($n2 - $n1), 6); + echo " $func {$time}s " . substr(md5(json_encode($res)), 0, 8) . "\r\n"; +} + +function bubbleSort($arr) +{ + $len = count($arr); + for ($i = 0; $i < $len; $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; +} + +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; +} + +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; +} + +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; +} + +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; +} + +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); +} + + +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; +} + +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; +} + +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; +} + +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; +} + +$total = 2000; + +sortTest('bubbleSort', $total); +sortTest('selectionSort', $total); +sortTest('insertionSort', $total); +sortTest('shellSort', $total); +sortTest('mergeSort', $total); +sortTest('quickSort', $total); +sortTest('heapSort', $total); +sortTest('countingSort', $total); +sortTest('bucketSort', $total); +sortTest('radixSort', $total); From e100b91ddb51f7e4ce5e787e7800f5f1ac067f10 Mon Sep 17 00:00:00 2001 From: hustcc Date: Tue, 13 Nov 2018 21:03:15 +0800 Subject: [PATCH 02/21] change to lint-md --- .travis.yml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index b2b108c..eefc643 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ -sudo: required -language: python -python: - - "2.7" +language: node_js +node_js: + - "10" before_install: - - pip install hint -script: - - hint . \ No newline at end of file + - npm i lint-md +script: npx lint-md ./ From fef886aebc3a01eeb70c706cb04704abc4c21315 Mon Sep 17 00:00:00 2001 From: hustcc Date: Tue, 13 Nov 2018 21:05:27 +0800 Subject: [PATCH 03/21] -g --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index eefc643..8df8b91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,5 @@ language: node_js node_js: - "10" before_install: - - npm i lint-md + - npm i -g lint-md script: npx lint-md ./ From 6c42a4931b95bf369d19612445d89f61faf2589e Mon Sep 17 00:00:00 2001 From: hustcc Date: Wed, 14 Nov 2018 09:45:06 +0800 Subject: [PATCH 04/21] hint -> lint-md --- .travis.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8df8b91..117276a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,4 @@ node_js: - "10" before_install: - npm i -g lint-md -script: npx lint-md ./ +script: lint-md ./ diff --git a/README.md b/README.md index a14f9c1..4c36daa 100644 --- a/README.md +++ b/README.md @@ -64,4 +64,4 @@ GitBook 在线阅读地址:[https://sort.hust.cc/](https://sort.hust.cc/)。 -本项目使用 [hint](https://github.com/hustcc/hint) 进行中文 Markdown 文件的格式检查,务必在提交 Pr 之前,保证 Markdown 格式正确。 +本项目使用 [lint-md](https://github.com/hustcc/lint-md) 进行中文 Markdown 文件的格式检查,务必在提交 Pr 之前,保证 Markdown 格式正确。 From fd6155a8606330fce301a4bcfdd8f3717136ad91 Mon Sep 17 00:00:00 2001 From: gallonyin Date: Thu, 15 Nov 2018 12:35:46 +0800 Subject: [PATCH 05/21] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BD=92=E5=B9=B6?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=A9=BA=E9=97=B4=E4=BD=BF=E7=94=A8(?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81copy)=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java/main/MergeSort.java | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/java/main/MergeSort.java b/src/java/main/MergeSort.java index 7fec2b8..6457036 100644 --- a/src/java/main/MergeSort.java +++ b/src/java/main/MergeSort.java @@ -23,25 +23,25 @@ public int[] sort(int[] sourceArray) throws Exception { protected int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; - int i = 0; - while (left.length > 0 && right.length > 0) { - if (left[0] <= right[0]) { - result[i++] = left[0]; - left = Arrays.copyOfRange(left, 1, left.length); + int l = 0, r = 0, len = 0; + while (len < left.length + right.length) { + if (left[l] <= right[r]) { + result[len++] = left[l++]; + + if (l == left.length) { + for (int i = r; i < right.length; i++) { + result[len++] = right[r++]; + } + } } else { - result[i++] = right[0]; - right = Arrays.copyOfRange(right, 1, right.length); - } - } + result[len++] = right[r++]; - while (left.length > 0) { - result[i++] = left[0]; - left = Arrays.copyOfRange(left, 1, left.length); - } - - while (right.length > 0) { - result[i++] = right[0]; - right = Arrays.copyOfRange(right, 1, right.length); + if (r == right.length) { + for (int i = l; i < left.length; i++) { + result[len++] = left[l++]; + } + } + } } return result; From a33fef7acbfa09fc5eaa332bb36966e9c89b7d15 Mon Sep 17 00:00:00 2001 From: hustcc Date: Tue, 13 Aug 2019 17:47:40 +0800 Subject: [PATCH 06/21] fix(*): update lint-md-cli lint-md -> lint-md-cli --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 117276a..2b6c65f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,5 @@ language: node_js node_js: - "10" before_install: - - npm i -g lint-md + - npm i -g lint-md-cli script: lint-md ./ From 6cd1226d51169a68f9382a7460fa6db988741c47 Mon Sep 17 00:00:00 2001 From: kkxiaojun <985531883@qq.com> Date: Tue, 13 Aug 2019 17:51:12 +0800 Subject: [PATCH 07/21] Update 7.heapSort.md (#24) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update 7.heapSort.md 建议修改下第一步的说明 * Update 7.heapSort.md --- 7.heapSort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/7.heapSort.md b/7.heapSort.md index 35b24ec..394d929 100644 --- a/7.heapSort.md +++ b/7.heapSort.md @@ -10,7 +10,7 @@ ## 1. 算法步骤 -1. 创建一个堆 H[0……n-1]; +1. 将待排序序列构建成一个堆 H[0……n-1],根据(升序降序需求)选择大顶堆或小顶堆; 2. 把堆首(最大值)和堆尾互换; @@ -254,4 +254,4 @@ function heapSort($arr) { } return $arr; } -``` \ No newline at end of file +``` From a2443c89e45aa25d31d77f6a46dc1d4a3f352570 Mon Sep 17 00:00:00 2001 From: TonyHe Date: Tue, 13 Aug 2019 19:24:17 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20PHP=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=83=A8=E5=88=86=E9=94=99=E8=AF=AF=20(#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 8.countingSort.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/8.countingSort.md b/8.countingSort.md index efabf9c..d0930cf 100644 --- a/8.countingSort.md +++ b/8.countingSort.md @@ -149,7 +149,11 @@ function countingSort($arr, $maxValue = null) $sortedIndex = 0; foreach ($bucket as $key => $len) { - if ($len !== null) $arr[$sortedIndex++] = $key; + if($len !== null){ + for($j = 0; $j < $len; $j++){ + $arr[$sortedIndex++] = $key; + } + } } return $arr; From 545fa6de16e7bf84aa697cc3df5d6b6364bfb936 Mon Sep 17 00:00:00 2001 From: hoop208 <137478244@qq.com> Date: Tue, 13 Aug 2019 19:30:44 +0800 Subject: [PATCH 09/21] Update 4.shellSort.md (#36) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正希尔排序java实现的gap计算 --- 4.shellSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/4.shellSort.md b/4.shellSort.md index 187ac0e..cfa59f5 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -99,7 +99,7 @@ public class ShellSort implements IArraySort { int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); int gap = 1; - while (gap < arr.length) { + while (gap < arr.length/3) { gap = gap * 3 + 1; } From bee257b71af4b1766ca795f4437ada21270786e7 Mon Sep 17 00:00:00 2001 From: hustcc Date: Thu, 22 Aug 2019 19:05:24 +0800 Subject: [PATCH 10/21] Create FUNDING.yml --- .github/FUNDING.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..6e86d35 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: ['https://paypal.me/hustcc', 'https://atool.vip'] From 00f39c74fa039f9faa1727e570a807f9d84003f4 Mon Sep 17 00:00:00 2001 From: goldSunshine <892697603@qq.com> Date: Tue, 3 Sep 2019 13:53:03 +0800 Subject: [PATCH 11/21] =?UTF-8?q?=E5=9F=BA=E6=95=B0=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0python=E5=AE=9E=E7=8E=B0=20(#31)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加python实现 * 基数排序格式调整 --- 10.radixSort.md | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/10.radixSort.md b/10.radixSort.md index 0bcd341..2da94b8 100644 --- a/10.radixSort.md +++ b/10.radixSort.md @@ -7,7 +7,7 @@ 基数排序有两种方法: -这三种排序算法都利用了桶的概念,但对桶的使用方法上有明显差异: +这三种排序算法都利用了桶的概念,但对桶的使用方法上有明显差异案例看大家发的: - 基数排序:根据键值的每位数字来分配桶; - 计数排序:每个桶只存储单一键值; @@ -49,7 +49,39 @@ function radixSort(arr, maxDigit) { } ``` -## 4. Java 代码实现 + +## 4. python 代码实现 + +```python +def radix(arr): + + digit = 0 + max_digit = 1 + max_value = max(arr) + #找出列表中最大的位数 + while 10**max_digit < max_value: + max_digit = max_digit + 1 + + while digit < max_digit: + temp = [[] for i in range(10)] + for i in arr: + #求出每一个元素的个、十、百位的值 + t = int((i/10**digit)%10) + temp[t].append(i) + + coll = [] + for bucket in temp: + for i in bucket: + coll.append(i) + + arr = coll + digit = digit + 1 + + return arr +``` + + +## 5. Java 代码实现 ```java /** @@ -134,7 +166,7 @@ public class RadixSort implements IArraySort { } ``` -## 5. PHP 代码实现 +## 6. PHP 代码实现 ```php function radixSort($arr, $maxDigit = null) @@ -169,4 +201,4 @@ function radixSort($arr, $maxDigit = null) return $arr; } -``` \ No newline at end of file +``` From 75a861bfd0f26c639a182fef2b5bdc4a5e4a90da Mon Sep 17 00:00:00 2001 From: nchhabra1311 Date: Wed, 9 Oct 2019 06:53:44 +0530 Subject: [PATCH 12/21] Added radix sort in pythonSortTest.py (#37) * Added radix sort in pythonSortTest.py * Improved code readability as suggested --- src/pythonSortTest.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/pythonSortTest.py b/src/pythonSortTest.py index ea765d2..bf092db 100644 --- a/src/pythonSortTest.py +++ b/src/pythonSortTest.py @@ -1,4 +1,4 @@ -''' +''' # Create by LokiSharp(loki.sharp#gmail) at 2017-1-22 ''' @@ -176,6 +176,34 @@ def countingSort(arr, maxValue=None): bucket[j] -= 1 return arr +def radix_count(exp1): + global list + n = len(list) + output = [0] * (n) + count = [0] * (10) + for i in range(0, n): + index = (list[i] / exp1) + count[(index) % 10] += 1 + for i in range(1,10): + count[i] += count[i - 1] + i = n - 1 + while i >= 0: + index = (list[i]/exp1) + output[count[(index) % 10] - 1] = list[i] + count[(index) % 10] -= 1 + i -= 1 + i = 0 + for i in range(0,len(list)): + list[i] = output[i] + +def radixSort(): + global list + max1 = max(list) + exp = 1 + while max1 / exp > 0: + radix_count(exp) + exp *= 10 + if __name__ == '__main__': sortTest(bubbleSort, TOTAL) @@ -185,3 +213,4 @@ def countingSort(arr, maxValue=None): sortTest(mergeSort, TOTAL) sortTest(quickSort, TOTAL) sortTest(heapSort, TOTAL) + sortTest(radixSort, TOTAL) From f89b90800f985d002a7f972088e95e254bdf3219 Mon Sep 17 00:00:00 2001 From: JulioFeng Date: Tue, 13 Apr 2021 16:20:52 +0800 Subject: [PATCH 13/21] Update 4.shellSort.md --- 4.shellSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/4.shellSort.md b/4.shellSort.md index cfa59f5..e1e99a3 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -69,7 +69,7 @@ def shellSort(arr): func shellSort(arr []int) []int { length := len(arr) gap := 1 - for gap < gap/3 { + for gap < length/3 { gap = gap*3 + 1 } for gap > 0 { From e1b1c66c569c4b9b5db7531b209382acad4922df Mon Sep 17 00:00:00 2001 From: shouao <792417939@qq.com> Date: Sat, 1 Jan 2022 06:34:55 +0800 Subject: [PATCH 14/21] add cplusplus version in shell-sort and merg-sort --- 4.shellSort.md | 20 ++++++++++++++++ 5.mergeSort.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/4.shellSort.md b/4.shellSort.md index cfa59f5..6f0bc7f 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -144,3 +144,23 @@ function shellSort($arr) return $arr; } ``` + +## 7. C++ 代码实现 + +```cpp +void shellSort(vector& nums) { + int gap = 1; + while (gap < (int)nums.size() / 3) { + gap = gap * 3 + 1; + } + for (; gap >= 1; gap /= 3) { + for (int i = 0; i < gap; ++i) { + for (int j = i + gap; j < nums.size(); j += gap) { + for (int k = j; k - gap >= 0 && nums[k] < nums[k - gap]; k -= gap) { + swap(nums[k], nums[k - gap]); + } + } + } + } +} +``` \ No newline at end of file diff --git a/5.mergeSort.md b/5.mergeSort.md index bf0de32..f49f547 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -222,4 +222,69 @@ function merge($left, $right) return $result; } +``` + +## 9. C++ 代码实现 + +### 9.1 递归版 + +```cpp +void mergeSort(vector& nums, int l, int r) { + if (r - l <= 1) { + return; + } + int mid = (l + r) / 2; + mergeSort(nums, l, mid); + mergeSort(nums, mid, r); + int index = 0; + int ptrL = l; + int ptrR = mid; + static vectortempary(50000); + while (ptrL != mid && ptrR != r) { + if (nums[ptrL] < nums[ptrR]) { + tempary[index++] = nums[ptrL++]; + } else { + tempary[index++] = nums[ptrR++]; + } + } + while (ptrL != mid) { + tempary[index++] = nums[ptrL++]; + } + while (ptrR != r) { + tempary[index++] = nums[ptrR++]; + } + copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); +} +``` + +### 9.2 迭代版 + +```cpp +void mergeSort(vector& nums) { + for (int step = 1; step <= nums.size(); step *= 2) { + for (int i = 0; i + step < nums.size(); i += 2 * step) { + int l = i; + int r = min((int)nums.size(), i + step * 2); + int mid = i + step; + int index = 0; + int ptrL = l; + int ptrR = mid; + static vectortempary(50000); + while (ptrL != mid && ptrR != r) { + if (nums[ptrL] < nums[ptrR]) { + tempary[index++] = nums[ptrL++]; + } else { + tempary[index++] = nums[ptrR++]; + } + } + while (ptrL != mid) { + tempary[index++] = nums[ptrL++]; + } + while (ptrR != r) { + tempary[index++] = nums[ptrR++]; + } + copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); + } + } +} ``` \ No newline at end of file From 3125abbfb3af6171f3746cd13422aebf139af7eb Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 06:42:15 +0800 Subject: [PATCH 15/21] Update 4.shellSort.md --- 4.shellSort.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/4.shellSort.md b/4.shellSort.md index 6f0bc7f..fca5e60 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -153,14 +153,15 @@ void shellSort(vector& nums) { while (gap < (int)nums.size() / 3) { gap = gap * 3 + 1; } - for (; gap >= 1; gap /= 3) { - for (int i = 0; i < gap; ++i) { - for (int j = i + gap; j < nums.size(); j += gap) { - for (int k = j; k - gap >= 0 && nums[k] < nums[k - gap]; k -= gap) { - swap(nums[k], nums[k - gap]); + for (; gap >= 1; gap /= 3) { + for (int i = 0; i < gap; ++i) { + for (int j = i + gap; j < nums.size(); j += gap) { + for (int k = j; k - gap >= 0 && nums[k] < nums[k - gap]; k -= gap) { + swap(nums[k], nums[k - gap]); } } } } } -``` \ No newline at end of file + +``` From 6773da4867c9c79fa3a108fca8bd511b6a342dfb Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 06:47:37 +0800 Subject: [PATCH 16/21] Update 5.mergeSort.md --- 5.mergeSort.md | 51 ++++++++++---------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index f49f547..c7b2622 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -226,16 +226,8 @@ function merge($left, $right) ## 9. C++ 代码实现 -### 9.1 递归版 - ```cpp -void mergeSort(vector& nums, int l, int r) { - if (r - l <= 1) { - return; - } - int mid = (l + r) / 2; - mergeSort(nums, l, mid); - mergeSort(nums, mid, r); +void merge(vector& nums, int l, int mid, int r) { int index = 0; int ptrL = l; int ptrR = mid; @@ -251,40 +243,17 @@ void mergeSort(vector& nums, int l, int r) { tempary[index++] = nums[ptrL++]; } while (ptrR != r) { - tempary[index++] = nums[ptrR++]; + tempary[index++] = nums[ptrR++]; } copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); } -``` - -### 9.2 迭代版 - -```cpp -void mergeSort(vector& nums) { - for (int step = 1; step <= nums.size(); step *= 2) { - for (int i = 0; i + step < nums.size(); i += 2 * step) { - int l = i; - int r = min((int)nums.size(), i + step * 2); - int mid = i + step; - int index = 0; - int ptrL = l; - int ptrR = mid; - static vectortempary(50000); - while (ptrL != mid && ptrR != r) { - if (nums[ptrL] < nums[ptrR]) { - tempary[index++] = nums[ptrL++]; - } else { - tempary[index++] = nums[ptrR++]; - } - } - while (ptrL != mid) { - tempary[index++] = nums[ptrL++]; - } - while (ptrR != r) { - tempary[index++] = nums[ptrR++]; - } - copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); - } +void mergeSort(vector& nums, int l, int r) { + if (r - l <= 1) { + return; } + int mid = (l + r) / 2; + mergeSort(nums, l, mid); + mergeSort(nums, mid, r); + merge(nums, l, mid, r); } -``` \ No newline at end of file +``` From 0c614a86da0d9e98395091e9173bcadb9283ec2c Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 06:57:11 +0800 Subject: [PATCH 17/21] Update 5.mergeSort.md --- 5.mergeSort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index c7b2622..c98680b 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -247,7 +247,7 @@ void merge(vector& nums, int l, int mid, int r) { } copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); } -void mergeSort(vector& nums, int l, int r) { +void mergeSort(vector& nums, int l, int r) {// range is [l, r), so if there is a 0-index array arr whose length is n, "mergeSort(arr, 0, n)" should be called if (r - l <= 1) { return; } From f62636bede725aa4068b5ba3928d8d65392ee0e8 Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 06:59:10 +0800 Subject: [PATCH 18/21] Update 5.mergeSort.md --- 5.mergeSort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index c98680b..d28fb29 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -247,8 +247,8 @@ void merge(vector& nums, int l, int mid, int r) { } copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); } -void mergeSort(vector& nums, int l, int r) {// range is [l, r), so if there is a 0-index array arr whose length is n, "mergeSort(arr, 0, n)" should be called - if (r - l <= 1) { +void mergeSort(vector& nums, int l, int r) {// sort the range [l, r), so if there is a 0-index array arr whose length is n + if (r - l <= 1) { //and you want sort the whole array, "mergeSort(arr, 0, n)" should be called return; } int mid = (l + r) / 2; From 4534fa5eff5ec2a11937c42358c1537b36dd60ae Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 07:04:15 +0800 Subject: [PATCH 19/21] Update 5.mergeSort.md --- 5.mergeSort.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index d28fb29..e55b90c 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -231,7 +231,8 @@ void merge(vector& nums, int l, int mid, int r) { int index = 0; int ptrL = l; int ptrR = mid; - static vectortempary(50000); + static vectortempary; + tempary.resize(nums.size()); while (ptrL != mid && ptrR != r) { if (nums[ptrL] < nums[ptrR]) { tempary[index++] = nums[ptrL++]; From 1f7b01b699f921b36e9d02900a7243de3a1e22d5 Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 07:18:06 +0800 Subject: [PATCH 20/21] Update 5.mergeSort.md --- 5.mergeSort.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/5.mergeSort.md b/5.mergeSort.md index e55b90c..c030d8f 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -227,34 +227,36 @@ function merge($left, $right) ## 9. C++ 代码实现 ```cpp -void merge(vector& nums, int l, int mid, int r) { +void merge(vector& arr, int l, int mid, int r) { int index = 0; int ptrL = l; int ptrR = mid; static vectortempary; - tempary.resize(nums.size()); + if (arr.size() > tempary.size()) { + tempary.resize(arr.size()); + } while (ptrL != mid && ptrR != r) { - if (nums[ptrL] < nums[ptrR]) { - tempary[index++] = nums[ptrL++]; + if (arr[ptrL] < arr[ptrR]) { + tempary[index++] = arr[ptrL++]; } else { - tempary[index++] = nums[ptrR++]; + tempary[index++] = arr[ptrR++]; } } while (ptrL != mid) { - tempary[index++] = nums[ptrL++]; + tempary[index++] = arr[ptrL++]; } while (ptrR != r) { - tempary[index++] = nums[ptrR++]; + tempary[index++] = arr[ptrR++]; } - copy(tempary.begin(), tempary.begin() + index, nums.begin() + l); + copy(tempary.begin(), tempary.begin() + index, arr.begin() + l); } -void mergeSort(vector& nums, int l, int r) {// sort the range [l, r), so if there is a 0-index array arr whose length is n - if (r - l <= 1) { //and you want sort the whole array, "mergeSort(arr, 0, n)" should be called +void mergeSort(vector& arr, int l, int r) { // sort the range [l, r) in arr + if (r - l <= 1) { return; } int mid = (l + r) / 2; - mergeSort(nums, l, mid); - mergeSort(nums, mid, r); - merge(nums, l, mid, r); + mergeSort(arr, l, mid); + mergeSort(arr, mid, r); + merge(arr, l, mid, r); } ``` From 75cb7d6954aead08bb90d934e46655a8307d1d13 Mon Sep 17 00:00:00 2001 From: shouao <58040617+chenshouao@users.noreply.github.com> Date: Sat, 1 Jan 2022 07:19:37 +0800 Subject: [PATCH 21/21] Update 4.shellSort.md --- 4.shellSort.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/4.shellSort.md b/4.shellSort.md index fca5e60..858a78f 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -148,20 +148,19 @@ function shellSort($arr) ## 7. C++ 代码实现 ```cpp -void shellSort(vector& nums) { +void shellSort(vector& arr) { int gap = 1; - while (gap < (int)nums.size() / 3) { + while (gap < (int)arr.size() / 3) { gap = gap * 3 + 1; } for (; gap >= 1; gap /= 3) { for (int i = 0; i < gap; ++i) { - for (int j = i + gap; j < nums.size(); j += gap) { - for (int k = j; k - gap >= 0 && nums[k] < nums[k - gap]; k -= gap) { - swap(nums[k], nums[k - gap]); + for (int j = i + gap; j < arr.size(); j += gap) { + for (int k = j; k - gap >= 0 && arr[k] < arr[k - gap]; k -= gap) { + swap(arr[k], arr[k - gap]); } } } } } - ```