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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Sorting/BogoSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class BogoSort {
// The array to be sorted
private array $array;

// Constructor to initialize the array
public function __construct(array $array) {
$this->array = $array;
}

// Method to check if the array is sorted
private function isSorted(): bool {
$count = count($this->array);
for ($i = 0; $i < $count - 1; $i++) {
if ($this->array[$i] > $this->array[$i + 1]) {
return false;
}
}
return true;
}

// Method to shuffle the array
private function shuffleArray(): void {
shuffle($this->array); // Built-in PHP shuffle function
}

// Method to sort the array using Bogo Sort
public function sort(): array {
while (!$this->isSorted()) {
$this->shuffleArray();
}
return $this->array;
}

// Method to get the sorted array
public function getSortedArray(): array {
return $this->sort();
}
}

// Example usage:
$array = [3, 1, 4, 1, 5];
$bogoSort = new BogoSort($array);

$sortedArray = $bogoSort->getSortedArray();
print_r($sortedArray);
Comment on lines +42 to +47
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
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: