Create BogoSort.php - #167
tusharnain wants to merge 1 commit into
Conversation
Added BogoSort
| // Example usage: | ||
| $array = [3, 1, 4, 1, 5]; | ||
| $bogoSort = new BogoSort($array); | ||
|
|
||
| $sortedArray = $bogoSort->getSortedArray(); | ||
| print_r($sortedArray); |
There was a problem hiding this comment.
Please create proper unit tests in the existing tests directory and the proper subdirectory
There was a problem hiding this comment.
Please create proper unit tests in the existing tests directory and the proper subdirectory
I can complement this PR:
tests/Sorting/BogoSortTest.php:
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Sorting/BogoSort.php';
use PHPUnit\Framework\TestCase;
class BogoSortTest extends TestCase
{
public function testAlreadySortedArray()
{
$sortedArray = [1, 2, 3, 4, 5];
$bogoSort = new BogoSort($sortedArray);
$this->assertSame($sortedArray, $bogoSort->getSortedArray(), "Not sorted properly");
}
public function testUnsortedArray()
{
$unsortedArray = [5, 3, 1, 4, 2];
$bogoSort = new BogoSort($unsortedArray);
$this->assertSame([1, 2, 3, 4, 5], $bogoSort->getSortedArray(), "Not sorted properly");
}
// Test sorting an array with duplicate values
public function testArrayWithDuplicates()
{
$arrayWithDuplicates = [4, 2, 3, 2, 1, 3];
$bogoSort = new BogoSort($arrayWithDuplicates);
$this->assertSame([1, 2, 2, 3, 3, 4], $bogoSort->getSortedArray(), "Not sorted properly");
}
// Test sorting an empty array
public function testEmptyArray()
{
$emptyArray = [];
$bogoSort = new BogoSort($emptyArray);
$this->assertSame($emptyArray, $bogoSort->getSortedArray(), "Not sorted properly");
}
// Test sorting a single element array
public function testSingleElementArray()
{
$singleElementArray = [42];
$bogoSort = new BogoSort($singleElementArray);
$this->assertSame($singleElementArray, $bogoSort->getSortedArray(), "Not sorted properly");
}
}DIRECTORY.md:
Sorting:
Tests:
darwinz
left a comment
There was a problem hiding this comment.
Please add a reference to this in Directory.md. Also, please add proper unit tests, documentation, and ensure all your code is linted (vendor/bin/phpcs -n)
| // Example usage: | ||
| $array = [3, 1, 4, 1, 5]; | ||
| $bogoSort = new BogoSort($array); | ||
|
|
||
| $sortedArray = $bogoSort->getSortedArray(); | ||
| print_r($sortedArray); |
There was a problem hiding this comment.
Please create proper unit tests in the existing tests directory and the proper subdirectory
I can complement this PR:
tests/Sorting/BogoSortTest.php:
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Sorting/BogoSort.php';
use PHPUnit\Framework\TestCase;
class BogoSortTest extends TestCase
{
public function testAlreadySortedArray()
{
$sortedArray = [1, 2, 3, 4, 5];
$bogoSort = new BogoSort($sortedArray);
$this->assertSame($sortedArray, $bogoSort->getSortedArray(), "Not sorted properly");
}
public function testUnsortedArray()
{
$unsortedArray = [5, 3, 1, 4, 2];
$bogoSort = new BogoSort($unsortedArray);
$this->assertSame([1, 2, 3, 4, 5], $bogoSort->getSortedArray(), "Not sorted properly");
}
// Test sorting an array with duplicate values
public function testArrayWithDuplicates()
{
$arrayWithDuplicates = [4, 2, 3, 2, 1, 3];
$bogoSort = new BogoSort($arrayWithDuplicates);
$this->assertSame([1, 2, 2, 3, 3, 4], $bogoSort->getSortedArray(), "Not sorted properly");
}
// Test sorting an empty array
public function testEmptyArray()
{
$emptyArray = [];
$bogoSort = new BogoSort($emptyArray);
$this->assertSame($emptyArray, $bogoSort->getSortedArray(), "Not sorted properly");
}
// Test sorting a single element array
public function testSingleElementArray()
{
$singleElementArray = [42];
$bogoSort = new BogoSort($singleElementArray);
$this->assertSame($singleElementArray, $bogoSort->getSortedArray(), "Not sorted properly");
}
}DIRECTORY.md:
Sorting:
Tests:
Bogo Sort is a highly inefficient sorting algorithm that randomly shuffles elements until they happen to be sorted. Although it's more of a joke algorithm due to its impracticality, here's a PHP implementation for Bogo Sort.