-
-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create BogoSort.php #167
base: master
Are you sure you want to change the base?
Create BogoSort.php #167
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please create proper unit tests in the existing tests directory and the proper subdirectory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.