diff --git a/DIRECTORY.md b/DIRECTORY.md index 7b41432b..850be426 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -47,6 +47,7 @@ * [Quicksort](./Sorting/QuickSort.php) * [Radixsort](./Sorting/RadixSort.php) * [Selectionsort](./Sorting/SelectionSort.php) + * [Shellsort](./Sorting/ShellSort.php) ## Strings * [Checkanagram](./Strings/CheckAnagram.php) @@ -75,6 +76,7 @@ * Sorting * [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php) * [Sortingtests](./tests/Sorting/SortingTests.php) + * [Shelltest](./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..cf9f915c --- /dev/null +++ b/Sorting/ShellSort.php @@ -0,0 +1,31 @@ + 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; +} +?> 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