Created new sort algorithm

added gnome sort algorithm, with added unit tests and updated DIRECTORY.md
This commit is contained in:
TKF486 2022-08-03 11:37:25 +08:00
parent 42c2656487
commit c97dadec46
3 changed files with 134 additions and 67 deletions

View File

@ -1,84 +1,94 @@
# List of all files
## Ciphers
* [Caesarcipher](./Ciphers/CaesarCipher.php)
* [Monoalphabeticcipher](./Ciphers/MonoAlphabeticCipher.php)
* [Morsecode](./Ciphers/MorseCode.php)
* [Xorcipher](./Ciphers/XORCipher.php)
- [Caesarcipher](./Ciphers/CaesarCipher.php)
- [Monoalphabeticcipher](./Ciphers/MonoAlphabeticCipher.php)
- [Morsecode](./Ciphers/MorseCode.php)
- [Xorcipher](./Ciphers/XORCipher.php)
## Conversions
* [Binarytodecimal](./Conversions/BinaryToDecimal.php)
* [Decimaltobinary](./Conversions/DecimalToBinary.php)
* [Hexadecimaltodecimal](./Conversions/HexadecimalToDecimal.php)
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
- [Binarytodecimal](./Conversions/BinaryToDecimal.php)
- [Decimaltobinary](./Conversions/DecimalToBinary.php)
- [Hexadecimaltodecimal](./Conversions/HexadecimalToDecimal.php)
- [Octaltodecimal](./Conversions/OctalToDecimal.php)
## Maths
* [Absolutemax](./Maths/AbsoluteMax.php)
* [Absolutemin](./Maths/AbsoluteMin.php)
* [Checkprime](./Maths/CheckPrime.php)
* [Factorial](./Maths/Factorial.php)
* [Fastexponentiation](./Maths/FastExponentiation.php)
* [Fibonacci](./Maths/Fibonacci.php)
* [Fibonacci2](./Maths/Fibonacci2.php)
* [Perfectsquare](./Maths/PerfectSquare.php)
* Projecteuler
* [Problem1](./Maths/ProjectEuler/Problem1.php)
* [Problem2](./Maths/ProjectEuler/Problem2.php)
* [Problem3](./Maths/ProjectEuler/Problem3.php)
* [Problem5](./Maths/ProjectEuler/Problem5.php)
* [Problem6](./Maths/ProjectEuler/Problem6.php)
* [Problem7](./Maths/ProjectEuler/Problem7.php)
* [Problem9](./Maths/ProjectEuler/Problem9.php)
- [Absolutemax](./Maths/AbsoluteMax.php)
- [Absolutemin](./Maths/AbsoluteMin.php)
- [Checkprime](./Maths/CheckPrime.php)
- [Factorial](./Maths/Factorial.php)
- [Fastexponentiation](./Maths/FastExponentiation.php)
- [Fibonacci](./Maths/Fibonacci.php)
- [Fibonacci2](./Maths/Fibonacci2.php)
- [Perfectsquare](./Maths/PerfectSquare.php)
- Projecteuler
- [Problem1](./Maths/ProjectEuler/Problem1.php)
- [Problem2](./Maths/ProjectEuler/Problem2.php)
- [Problem3](./Maths/ProjectEuler/Problem3.php)
- [Problem5](./Maths/ProjectEuler/Problem5.php)
- [Problem6](./Maths/ProjectEuler/Problem6.php)
- [Problem7](./Maths/ProjectEuler/Problem7.php)
- [Problem9](./Maths/ProjectEuler/Problem9.php)
## Searches
* [Binarysearch](./Searches/BinarySearch.php)
* [Fibonaccisearch](./Searches/FibonacciSearch.php)
* [Jumpsearch](./Searches/JumpSearch.php)
* [Linearsearch](./Searches/LinearSearch.php)
* [Lowerbound](./Searches/LowerBound.php)
* [Upperbound](./Searches/UpperBound.php)
- [Binarysearch](./Searches/BinarySearch.php)
- [Fibonaccisearch](./Searches/FibonacciSearch.php)
- [Jumpsearch](./Searches/JumpSearch.php)
- [Linearsearch](./Searches/LinearSearch.php)
- [Lowerbound](./Searches/LowerBound.php)
- [Upperbound](./Searches/UpperBound.php)
## Sorting
* [Arraykeyssort](./Sorting/ArrayKeysSort.php)
* [Bubblesort](./Sorting/BubbleSort.php)
* [Bubblesort2](./Sorting/BubbleSort2.php)
* [Countsort](./Sorting/CountSort.php)
* [Insertionsort](./Sorting/InsertionSort.php)
* [Mergesort](./Sorting/MergeSort.php)
* [Quicksort](./Sorting/QuickSort.php)
* [Radixsort](./Sorting/RadixSort.php)
* [Selectionsort](./Sorting/SelectionSort.php)
- [Arraykeyssort](./Sorting/ArrayKeysSort.php)
- [Bubblesort](./Sorting/BubbleSort.php)
- [Bubblesort2](./Sorting/BubbleSort2.php)
- [Countsort](./Sorting/CountSort.php)
- [Gnomesort](./Sorting/GnomeSort.php)
- [Insertionsort](./Sorting/InsertionSort.php)
- [Mergesort](./Sorting/MergeSort.php)
- [Quicksort](./Sorting/QuickSort.php)
- [Radixsort](./Sorting/RadixSort.php)
- [Selectionsort](./Sorting/SelectionSort.php)
## Strings
* [Checkanagram](./Strings/CheckAnagram.php)
* [Checkpalindrome](./Strings/CheckPalindrome.php)
* [Checkpalindrome2](./Strings/CheckPalindrome2.php)
* [Countconsonants](./Strings/CountConsonants.php)
* [Countsentences](./Strings/CountSentences.php)
* [Countvowels](./Strings/CountVowels.php)
* [Distance](./Strings/Distance.php)
* [Maxcharacter](./Strings/MaxCharacter.php)
* [Reversestring](./Strings/ReverseString.php)
* [Reversewords](./Strings/ReverseWords.php)
- [Checkanagram](./Strings/CheckAnagram.php)
- [Checkpalindrome](./Strings/CheckPalindrome.php)
- [Checkpalindrome2](./Strings/CheckPalindrome2.php)
- [Countconsonants](./Strings/CountConsonants.php)
- [Countsentences](./Strings/CountSentences.php)
- [Countvowels](./Strings/CountVowels.php)
- [Distance](./Strings/Distance.php)
- [Maxcharacter](./Strings/MaxCharacter.php)
- [Reversestring](./Strings/ReverseString.php)
- [Reversewords](./Strings/ReverseWords.php)
## Tests
* Ciphers
* [Cipherstest](./tests/Ciphers/CiphersTest.php)
* [Monoalphabeticciphertest](./tests/Ciphers/MonoAlphabeticCipherTest.php)
* [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
* Conversions
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
* Maths
* [Mathstest](./tests/Maths/MathsTest.php)
* [Projecteulertest](./tests/Maths/ProjectEulerTest.php)
* Searches
* [Searchestest](./tests/Searches/SearchesTest.php)
* Sorting
* [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php)
* [Sortingtests](./tests/Sorting/SortingTests.php)
* Strings
* [Stringstest](./tests/Strings/StringsTest.php)
- Ciphers
- [Cipherstest](./tests/Ciphers/CiphersTest.php)
- [Monoalphabeticciphertest](./tests/Ciphers/MonoAlphabeticCipherTest.php)
- [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
- Conversions
- [Conversionstest](./tests/Conversions/ConversionsTest.php)
- Maths
- [Mathstest](./tests/Maths/MathsTest.php)
- [Projecteulertest](./tests/Maths/ProjectEulerTest.php)
- Searches
- [Searchestest](./tests/Searches/SearchesTest.php)
- Sorting
- [Arraykeyssorttest](./tests/Sorting/ArrayKeysSortTest.php)
- [Gnomesorttest](./tests/Sorting/GnomeSortTest.php)
- [Sortingtests](./tests/Sorting/SortingTests.php)
- Strings
- [Stringstest](./tests/Strings/StringsTest.php)
## Utils
* [Arrayhelpers](./Utils/ArrayHelpers.php)
* [Executiontime](./Utils/ExecutionTime.php)
- [Arrayhelpers](./Utils/ArrayHelpers.php)
- [Executiontime](./Utils/ExecutionTime.php)

34
Sorting/GnomeSort.php Normal file
View File

@ -0,0 +1,34 @@
<?php
/**
* Gnome Sort
* References:
* https://www.geeksforgeeks.org/gnome-sort-a-stupid-one/
*
* The Gnome algorithm works by locating the first instance in which two adjoining elements are arranged incorrectly and swaps with each other.
*
* @param array $array refers to the array to be sorted
* @return array
*/
function gnomeSort($array){
$a = 1;
$b = 2;
while($a < count($array)){
if ($array[$a-1] <= $array[$a]){
$a = $b;
$b++;
}else{
list($array[$a],$array[$a-1]) = array($array[$a-1],$array[$a]);
$a--;
if($a == 0){
$a = $b;
$b++;
}
}
}
return $array;
}

View File

@ -0,0 +1,23 @@
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Sorting/GnomeSort.php';
use PHPUnit\Framework\TestCase;
class gnomeSortTest extends TestCase
{
public function testGnomeSort()
{
$array = array(3, 0, 2, 5, -1, 4, 1);
$sorted = gnomeSort($array);
$this->assertEquals([-1, 0, 1, 2, 3, 4, 5], $sorted);
}
public function testGnomeSort2()
{
$array = array(8, -50, 1, 90, 35, 47, 69, -5);
$sorted = gnomeSort($array);
$this->assertEquals([-50, -5, 1, 8, 35, 47, 69, 90], $sorted);
}
}