Skip to content

baygin/php-search-algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Search Algorithms Implementation in PHP 8

The search algorithm implementation in PHP 8 to find the value with the query as fast.

Usage

Install

Git Clone

git clone https://github.com/baygin/php-search-algorithms.git

Composer

composer require baygin/php-search-algorithms

Binary Search

Array

$array = [];

for ($index = 0; $index < 100 * 10000; $index++) {
    $array[] = $index + 1;
}

$search = new BinarySearch();

$search->setCompareCallback(fn ($current, $searchValue) => $current === $searchValue)
    ->setDirectionCallback(fn ($current, $searchValue) => $current < $searchValue)
    ->setArray($array)
    ->setSearchValue(98589)
    ->search();

$foundIndex = $search->getFoundIndex();
$foundValue = $search->getFoundValue();

Arrays in array

$array = [];

for ($index = 0; $index < 100 * 10000; $index++) {
    $array[] = [
        "id" => $index + 1,
        "first" => "Baris {$index}",
        "last" => "Manco {$index}",
    ];
}

$search = new BinarySearch();

$search->setCompareCallback(fn ($current, $searchValue) => $current["id"] === $searchValue)
    ->setDirectionCallback(fn ($current, $searchValue) => $current["id"] < $searchValue)
    ->setArray($array)
    ->setSearchValue(81300)
    ->search();

$foundIndex = $search->getFoundIndex();
$foundValue = $search->getFoundValue();

Objects in array

$array = [];

for ($index = 0; $index < 100 * 10000; $index++) {
    $array[] = (object) [
        "id" => $index + 1,
        "first" => "Baris {$index}",
        "last" => "Manco {$index}",
    ];
}

$search = new BinarySearch();
$search->setCompareCallback(fn ($current, $searchValue) => $current->id === $searchValue)
    ->setDirectionCallback(fn ($current, $searchValue) => $current->id < $searchValue)
    ->setArray($array)
    ->setSearchValue(81300)
    ->search();

$foundIndex = $search->getFoundIndex();
$foundValue = $search->getFoundValue();

Tests

composer test

Contributing

If you want to contribute to the development of this library, you can open an issue or submit a pull request.

License

Licensed under the GPL3. See LICENSE for more information.

About

The search algorithms implementations in PHP 8

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages