From 4e4e509640684bb3cc00d06dc0422b258beccf2c Mon Sep 17 00:00:00 2001 From: Desmond325 Date: Wed, 3 Aug 2022 22:46:37 +0800 Subject: [PATCH 1/8] Added CombSort algorithm add comb sort algorithm and add up comb sort testing --- DIRECTORY.md | 2 ++ Sorting/CombSort.php | 31 +++++++++++++++++++++++++++++++ tests/Sorting/CombSortTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 Sorting/CombSort.php create mode 100644 tests/Sorting/CombSortTest.php diff --git a/DIRECTORY.md b/DIRECTORY.md index 7b41432b..ea747cf8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -42,6 +42,7 @@ * [Bubblesort](./Sorting/BubbleSort.php) * [Bubblesort2](./Sorting/BubbleSort2.php) * [Countsort](./Sorting/CountSort.php) + * [Combsort](./Sorting/CombSort.php) * [Insertionsort](./Sorting/InsertionSort.php) * [Mergesort](./Sorting/MergeSort.php) * [Quicksort](./Sorting/QuickSort.php) @@ -74,6 +75,7 @@ * [Searchestest](./tests/Searches/SearchesTest.php) * Sorting * [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php) + * [CombSorttest](./tests/Sorting/CombSortTest.php) * [Sortingtests](./tests/Sorting/SortingTests.php) * Strings * [Stringstest](./tests/Strings/StringsTest.php) diff --git a/Sorting/CombSort.php b/Sorting/CombSort.php new file mode 100644 index 00000000..f769d49b --- /dev/null +++ b/Sorting/CombSort.php @@ -0,0 +1,31 @@ + 1 || $s){ + if($g > 1) $g /= 1.25; + $s = false; + $j = 0; + while($j+$g < count($array)){ + if($array[$j] > $array[$j+$g]){ + list($array[$j], $array[$j+$g]) = array($array[$j+$g],$array[$j]); + $s = true; + } + $j++; + } + } + return $array; +} +?> \ No newline at end of file diff --git a/tests/Sorting/CombSortTest.php b/tests/Sorting/CombSortTest.php new file mode 100644 index 00000000..b4a7c4f0 --- /dev/null +++ b/tests/Sorting/CombSortTest.php @@ -0,0 +1,24 @@ +assertEquals([-2, 1, 2, 4, 5, 7, 8], $sorted); + } + + public function testCombSort2() + { + $array = array(-4, -1, 8, 0, 5, 3, 1); + $sorted = gnomeSort($array); + $this->assertEquals([-4, -1, 0, 1, 3, 5, 8], $sorted); + } + +} \ No newline at end of file From 4b79b720658a8d4b9322ddf86690161eea74f7f3 Mon Sep 17 00:00:00 2001 From: Desmond325 Date: Thu, 4 Aug 2022 23:18:10 +0800 Subject: [PATCH 2/8] Comb Sort comb sort algorithm --- tests/Sorting/CombSortTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Sorting/CombSortTest.php b/tests/Sorting/CombSortTest.php index b4a7c4f0..4cb4eee4 100644 --- a/tests/Sorting/CombSortTest.php +++ b/tests/Sorting/CombSortTest.php @@ -16,9 +16,9 @@ public function testCombSort() public function testCombSort2() { - $array = array(-4, -1, 8, 0, 5, 3, 1); + $array = array(-4, -1, 7, 0, 5, 3, 1); $sorted = gnomeSort($array); - $this->assertEquals([-4, -1, 0, 1, 3, 5, 8], $sorted); + $this->assertEquals([-4, -1, 0, 1, 3, 5, 7], $sorted); } } \ No newline at end of file From 2748ebbb2fcbde5a06623c63fdb8856f4481d042 Mon Sep 17 00:00:00 2001 From: Desmond325 Date: Sat, 6 Aug 2022 16:09:36 +0800 Subject: [PATCH 3/8] test script change testing code --- tests/Sorting/CombSortTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Sorting/CombSortTest.php b/tests/Sorting/CombSortTest.php index 4cb4eee4..83e81d41 100644 --- a/tests/Sorting/CombSortTest.php +++ b/tests/Sorting/CombSortTest.php @@ -10,14 +10,14 @@ class combSortTest extends TestCase public function testCombSort() { $array = array(5, 2, 1, 7, -2, 8, 4); - $sorted = gnomeSort($array); + $sorted = combSort($array); $this->assertEquals([-2, 1, 2, 4, 5, 7, 8], $sorted); } public function testCombSort2() { $array = array(-4, -1, 7, 0, 5, 3, 1); - $sorted = gnomeSort($array); + $sorted = combSort($array); $this->assertEquals([-4, -1, 0, 1, 3, 5, 7], $sorted); } From 53bfd8c332633f85736467a5c303440f83be18fd Mon Sep 17 00:00:00 2001 From: Desmond325 Date: Sat, 6 Aug 2022 16:42:43 +0800 Subject: [PATCH 4/8] ShellSort Added Shell Sort Algorithm --- DIRECTORY.md | 2 ++ Sorting/ShellSort.php | 21 +++++++++++++++++++++ tests/Sorting/ShellSortTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 Sorting/ShellSort.php create mode 100644 tests/Sorting/ShellSortTest.php diff --git a/DIRECTORY.md b/DIRECTORY.md index ea747cf8..12a1b844 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -48,6 +48,7 @@ * [Quicksort](./Sorting/QuickSort.php) * [Radixsort](./Sorting/RadixSort.php) * [Selectionsort](./Sorting/SelectionSort.php) + * [Shellsort](./Sorting/ShellSort.php) ## Strings * [Checkanagram](./Strings/CheckAnagram.php) @@ -77,6 +78,7 @@ * [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php) * [CombSorttest](./tests/Sorting/CombSortTest.php) * [Sortingtests](./tests/Sorting/SortingTests.php) + * [CombSorttest](./tests/Sorting/ShellSortTest.php) * Strings * [Stringstest](./tests/Strings/StringsTest.php) diff --git a/Sorting/ShellSort.php b/Sorting/ShellSort.php new file mode 100644 index 00000000..8f556eab --- /dev/null +++ b/Sorting/ShellSort.php @@ -0,0 +1,21 @@ + 0) + { + for($b = $a; $b < count($array);$b++){ + $temp = $array[$b]; + $i = $b; + while($i >= $a && $array[$i-$a] > $temp) + { + $array[$i] = $array[$i - $a]; + $i -= $a; + } + $array[$i] = $temp; + } + $a = round($a/2.2); + } + return $array; +} +?> \ No newline at end of file diff --git a/tests/Sorting/ShellSortTest.php b/tests/Sorting/ShellSortTest.php new file mode 100644 index 00000000..e6c12932 --- /dev/null +++ b/tests/Sorting/ShellSortTest.php @@ -0,0 +1,24 @@ +assertEquals([0, 1, 2, 3, 5, 6, 9], $sorted); + } + + public function testShellSort2() + { + $array = array(7, 9, 1, 6, 2, 5, 0)); + $sorted = shellSort($array); + $this->assertEquals([0, 1, 2, 5, 6, 7, 9], $sorted); + } + +} \ No newline at end of file From 472094e006b28fd93bcccb2377c78b0417705a9a Mon Sep 17 00:00:00 2001 From: Desmond325 <87235152+Desmond325@users.noreply.github.com> Date: Sat, 6 Aug 2022 20:36:44 +0800 Subject: [PATCH 5/8] Delete CombSort.php --- Sorting/CombSort.php | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 Sorting/CombSort.php diff --git a/Sorting/CombSort.php b/Sorting/CombSort.php deleted file mode 100644 index f769d49b..00000000 --- a/Sorting/CombSort.php +++ /dev/null @@ -1,31 +0,0 @@ - 1 || $s){ - if($g > 1) $g /= 1.25; - $s = false; - $j = 0; - while($j+$g < count($array)){ - if($array[$j] > $array[$j+$g]){ - list($array[$j], $array[$j+$g]) = array($array[$j+$g],$array[$j]); - $s = true; - } - $j++; - } - } - return $array; -} -?> \ No newline at end of file From a407f2c6aac9465f236ce02e56b116860ee5fe99 Mon Sep 17 00:00:00 2001 From: Desmond325 <87235152+Desmond325@users.noreply.github.com> Date: Sat, 6 Aug 2022 20:37:14 +0800 Subject: [PATCH 6/8] Delete CombSortTest.php --- tests/Sorting/CombSortTest.php | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 tests/Sorting/CombSortTest.php diff --git a/tests/Sorting/CombSortTest.php b/tests/Sorting/CombSortTest.php deleted file mode 100644 index 83e81d41..00000000 --- a/tests/Sorting/CombSortTest.php +++ /dev/null @@ -1,24 +0,0 @@ -assertEquals([-2, 1, 2, 4, 5, 7, 8], $sorted); - } - - public function testCombSort2() - { - $array = array(-4, -1, 7, 0, 5, 3, 1); - $sorted = combSort($array); - $this->assertEquals([-4, -1, 0, 1, 3, 5, 7], $sorted); - } - -} \ No newline at end of file From 4e5e07906ad65e7ef29d906bf26a2fff6c27daa9 Mon Sep 17 00:00:00 2001 From: Desmond325 <87235152+Desmond325@users.noreply.github.com> Date: Sat, 6 Aug 2022 20:38:11 +0800 Subject: [PATCH 7/8] Update DIRECTORY.md --- DIRECTORY.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 12a1b844..850be426 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -42,7 +42,6 @@ * [Bubblesort](./Sorting/BubbleSort.php) * [Bubblesort2](./Sorting/BubbleSort2.php) * [Countsort](./Sorting/CountSort.php) - * [Combsort](./Sorting/CombSort.php) * [Insertionsort](./Sorting/InsertionSort.php) * [Mergesort](./Sorting/MergeSort.php) * [Quicksort](./Sorting/QuickSort.php) @@ -76,9 +75,8 @@ * [Searchestest](./tests/Searches/SearchesTest.php) * Sorting * [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php) - * [CombSorttest](./tests/Sorting/CombSortTest.php) * [Sortingtests](./tests/Sorting/SortingTests.php) - * [CombSorttest](./tests/Sorting/ShellSortTest.php) + * [Shelltest](./tests/Sorting/ShellSortTest.php) * Strings * [Stringstest](./tests/Strings/StringsTest.php) From e2a6529150f8de825e91a14b338daa24cf51232b Mon Sep 17 00:00:00 2001 From: Desmond325 <87235152+Desmond325@users.noreply.github.com> Date: Sun, 7 Aug 2022 01:01:51 +0800 Subject: [PATCH 8/8] update ShellSort Add some comments on the source code. --- Sorting/ShellSort.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sorting/ShellSort.php b/Sorting/ShellSort.php index 8f556eab..cf9f915c 100644 --- a/Sorting/ShellSort.php +++ b/Sorting/ShellSort.php @@ -1,4 +1,14 @@ \ No newline at end of file +?>