Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tusharnain4578
Copy link

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.

Added BogoSort
Comment on lines +42 to +47
// Example usage:
$array = [3, 1, 4, 1, 5];
$bogoSort = new BogoSort($array);

$sortedArray = $bogoSort->getSortedArray();
print_r($sortedArray);
Copy link
Contributor

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

Copy link
Contributor

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:

Copy link
Contributor

@darwinz darwinz left a 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)

Comment on lines +42 to +47
// Example usage:
$array = [3, 1, 4, 1, 5];
$bogoSort = new BogoSort($array);

$sortedArray = $bogoSort->getSortedArray();
print_r($sortedArray);
Copy link
Contributor

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants