Skip to content

Create BogoSort.php - #167

Closed
tusharnain wants to merge 1 commit into
TheAlgorithms:masterfrom
tusharnain:master
Closed

tusharnain wants to merge 1 commit into
TheAlgorithms:masterfrom
tusharnain:master

Conversation

@tusharnain

Copy link
Copy Markdown

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 thread Sorting/BogoSort.php
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
Copy Markdown
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
Copy Markdown
Member

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:

@darwinz darwinz left a comment

Copy link
Copy Markdown
Contributor

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 thread Sorting/BogoSort.php
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
Copy Markdown
Member

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:

@tusharnain tusharnain closed this by deleting the head repository May 26, 2025
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