Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/SelectionSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Src;

class SelectionSort
{
/**
* @param array<int> $arr
* @return array<int>
*/
public static function sort(array $arr): array
{
$length = count($arr);
for ($i = 0; $i < $length; $i++) {
$smallest = ['index' => $i, 'value' => $arr[$i]];
for ($j = $i + 1; $j < $length; $j++) {
if ($arr[$j] < $smallest['value']) {
$smallest = ['index' => $j, 'value' => $arr[$j]];
}
}
if ($smallest['index'] != $i) {
[$arr[$smallest['index']], $arr[$i]] = [$arr[$i], $arr[$smallest['index']]];
}
}
return $arr;
}
}
25 changes: 25 additions & 0 deletions tests/Benchmark/SelectionSortBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Benchmark;

use Generator;
use PhpBench\Attributes as Bench;
use Src\SelectionSort;

class SelectionSortBench
{
#[Bench\Revs(5)]
#[Bench\Iterations(5)]
#[Bench\ParamProviders(['provideArray'])]
#[Bench\Timeout(10.0)]
public function benchSelectionSort(array $params)
{
SelectionSort::sort($params['arr']);
}

public function provideArray(): Generator
{
yield '100' => ['arr' => [487235, 979261, 252505, 750338, 927605, 285052, 710411, 459024, 341306, 594800, 816357, 832689, 155988, 627930, 737987, 696919, 508496, 293227, 469227, 21472, 818492, 662549, 702043, 882995, 269164, 618273, 2422, 457017, 705142, 437709, 303161, 588406, 436348, 662449, 421115, 545405, 31185, 712979, 358940, 493500, 158880, 796027, 956441, 399102, 144518, 589990, 167708, 401210, 503377, 688391, 41503, 911757, 789607, 239914, 233908, 19659, 322164, 128946, 533689, 808714, 694216, 70080, 898202, 299221, 994540, 361355, 713460, 412031, 347017, 320308, 993281, 866683, 760297, 974850, 308393, 101476, 428070, 74127, 336612, 701999, 525677, 715462, 778861, 438076, 888997, 321941, 805740, 759803, 286249, 344464, 290207, 994106, 770710, 6650, 959936, 763813, 121686, 32591, 910440, 579755]];
yield '1000' => ['arr' => [834400, 333681, 281996, 178893, 390211, 529367, 400997, 215314, 962545, 309866, 676927, 876471, 876438, 812396, 86688, 474199, 615427, 585703, 665903, 204935, 102611, 812669, 581433, 564383, 290435, 489118, 672922, 470652, 850292, 964516, 63560, 244886, 320181, 899749, 411686, 752799, 846864, 7240, 668380, 951457, 625842, 52787, 635699, 47710, 914224, 811338, 685969, 222925, 790044, 426659, 614714, 906158, 987212, 805281, 679825, 839811, 498545, 671357, 772318, 83043, 403005, 680009, 224999, 505394, 951609, 348711, 688119, 13651, 359056, 802833, 460931, 18887, 572255, 86244, 138957, 606112, 453321, 40262, 788273, 235554, 264350, 527851, 579625, 933318, 902585, 501173, 427386, 487332, 169947, 457267, 400071, 133744, 394420, 370740, 180240, 194545, 917509, 157386, 656107, 152560, 211539, 894683, 870173, 883461, 271340, 96058, 77764, 148703, 445076, 648790, 103547, 10300, 692037, 326335, 462195, 991790, 24886, 972485, 64429, 277587, 514981, 687197, 279271, 466180, 948926, 270403, 898254, 691849, 670822, 117284, 551878, 129188, 396124, 981324, 128747, 579656, 677458, 28090, 612749, 368531, 672849, 583563, 566965, 596459, 522498, 578676, 434181, 5268, 157660, 858293, 119968, 771583, 182095, 253316, 415738, 669955, 205095, 782195, 718127, 441712, 398372, 940803, 48545, 352367, 244606, 75057, 971610, 741037, 240630, 874288, 36533, 357399, 687322, 630218, 194982, 419317, 639437, 287412, 102506, 871199, 587665, 568237, 320470, 93147, 971509, 949601, 801679, 722760, 908253, 619973, 815531, 292462, 309374, 681145, 705686, 829884, 439356, 821154, 35088, 490277, 364014, 116912, 351321, 142491, 587047, 120129, 492567, 1979, 453450, 723642, 618654, 289991, 42942, 966819, 335127, 344331, 620246, 838693, 358656, 753537, 756745, 302216, 496269, 4512, 795279, 245961, 546534, 263909, 460056, 859294, 114807, 944293, 411240, 184442, 88765, 366597, 289719, 527825, 73539, 895355, 230217, 49277, 167362, 743306, 872324, 602158, 608019, 485656, 967593, 738863, 200348, 803579, 448624, 653320, 967921, 403707, 635428, 208396, 920091, 826285, 454048, 432494, 419067, 8271, 799923, 210229, 587759, 512519, 287875, 462353, 328833, 169710, 733155, 366136, 529049, 544598, 639069, 625453, 319737, 378239, 14238, 305670, 446795, 877113, 449647, 263552, 692133, 153523, 385479, 743229, 134783, 70725, 124077, 937626, 385022, 483454, 293451, 315539, 928593, 384352, 570739, 59725, 715562, 941697, 112774, 930490, 742076, 738673, 199788, 252158, 530489, 358071, 973773, 554194, 446849, 67478, 912619, 690999, 748447, 973790, 88963, 145899, 672730, 274439, 751602, 408722, 757169, 38931, 528907, 802894, 197890, 215705, 520939, 406093, 635263, 180520, 725593, 750994, 856600, 53519, 411098, 274210, 545229, 818762, 344751, 657583, 216673, 304295, 711427, 823674, 640156, 413301, 30605, 583382, 451440, 177645, 913626, 55354, 182627, 125999, 796456, 353108, 579503, 312078, 388018, 221434, 623346, 95736, 209262, 951502, 148687, 947, 994095, 667069, 247576, 585861, 75093, 62627, 517207, 87314, 414946, 849801, 540858, 924345, 415151, 458908, 523273, 202947, 835185, 345958, 989464, 239976, 868012, 634148, 254486, 869295, 507970, 89849, 600317, 823670, 433755, 292678, 558900, 199503, 84001, 543500, 951964, 302441, 160360, 231937, 747950, 709489, 814250, 860914, 741559, 843556, 628763, 308990, 94592, 499738, 605955, 144354, 796065, 640464, 600435, 506856, 127810, 133896, 780635, 187243, 662203, 30029, 136012, 919625, 337869, 354017, 529350, 152351, 524904, 687500, 371429, 882289, 351416, 42381, 229314, 288664, 498374, 633351, 925084, 772400, 199410, 392319, 181587, 6207, 938064, 255761, 126793, 404307, 97286, 658884, 913941, 827980, 779949, 98437, 700365, 5871, 895418, 7290, 939250, 330802, 157743, 767469, 571080, 59417, 636630, 894674, 968494, 613870, 909977, 915320, 977372, 583351, 582760, 363686, 109633, 204280, 194532, 754571, 501499, 96848, 772291, 53588, 560289, 100450, 327198, 875344, 393660, 178697, 869820, 537029, 904317, 475324, 600913, 125932, 349737, 518776, 99252, 736715, 337325, 131259, 416965, 88797, 853517, 472510, 90280, 602211, 691146, 511415, 982661, 322139, 322501, 530506, 822816, 69842, 495064, 224373, 455037, 13157, 547470, 129089, 465015, 740634, 799169, 769254, 381635, 897074, 149287, 845971, 403015, 424126, 881504, 982647, 765610, 216203, 784315, 505550, 381528, 471137, 278212, 113065, 301727, 275805, 446868, 116525, 307337, 870131, 803733, 827530, 875554, 916920, 186798, 205766, 548204, 345092, 231361, 740244, 982301, 574572, 510006, 940612, 495487, 509877, 626853, 231462, 974567, 358625, 751062, 845496, 851010, 895282, 161938, 662338, 687655, 355758, 115039, 791064, 133641, 864933, 184284, 33425, 158695, 371590, 511098, 325777, 18069, 204702, 742310, 184430, 810763, 100358, 521589, 819540, 299434, 785547, 881569, 410789, 280014, 498690, 759934, 147366, 882663, 454774, 84558, 525574, 956752, 508425, 913605, 619708, 259486, 414423, 697585, 712485, 221488, 124408, 890681, 857604, 827765, 296606, 957953, 873833, 874640, 455981, 211678, 737458, 116677, 421267, 969552, 844098, 700748, 737733, 111700, 342810, 390767, 69714, 415218, 450347, 514790, 747583, 851624, 373753, 926911, 486573, 491160, 885335, 590472, 921353, 154712, 815462, 292863, 378790, 421701, 974852, 914640, 957603, 766415, 711267, 211540, 250411, 706254, 955931, 664804, 786429, 103450, 828245, 509486, 857792, 388316, 617095, 488751, 667030, 748055, 662804, 193219, 372640, 338979, 699780, 776263, 466160, 890104, 969932, 537974, 355557, 959895, 28217, 423984, 139057, 42712, 444753, 79847, 603858, 822011, 478936, 864971, 560554, 88849, 944471, 89602, 979063, 747417, 432094, 923703, 267040, 189402, 241787, 180539, 832830, 72827, 91159, 407120, 722316, 821650, 816651, 518717, 191652, 908603, 689716, 957409, 921264, 754211, 67424, 909197, 354609, 979779, 868415, 176948, 492770, 613902, 172578, 661831, 737552, 196920, 669062, 483426, 78547, 782892, 577308, 242551, 924559, 302944, 637422, 200365, 340496, 565475, 960316, 100483, 112641, 472537, 776400, 676728, 308584, 962293, 424231, 814925, 369089, 810177, 510264, 904537, 436280, 641132, 996799, 87080, 562474, 31968, 525864, 233902, 746249, 937076, 329542, 305225, 900571, 785447, 179758, 622751, 387964, 987667, 816033, 588737, 421009, 364082, 230049, 298373, 474854, 113641, 858550, 527668, 492295, 939084, 840184, 578936, 620495, 763155, 73100, 227997, 109055, 383852, 395174, 148528, 508065, 240615, 943984, 194025, 740739, 911112, 602874, 644744, 73446, 492473, 887059, 300791, 21269, 396163, 55586, 617897, 683377, 407678, 707929, 207659, 591510, 822037, 193842, 661001, 643462, 377547, 900535, 288939, 699877, 562193, 87467, 29867, 224084, 452866, 907594, 18191, 296592, 427023, 873673, 207172, 240710, 903089, 43620, 704172, 562946, 925250, 910888, 722631, 452314, 10589, 147633, 284110, 315122, 399075, 735570, 623729, 990308, 524135, 856009, 50061, 978318, 983137, 283874, 66471, 700095, 321631, 566758, 49427, 626598, 551458, 560429, 658008, 9912, 145746, 666284, 353573, 894298, 903617, 466554, 695433, 911825, 399714, 439516, 13449, 258252, 943023, 687746, 527888, 972360, 735902, 727901, 882979, 189248, 326365, 467332, 959826, 164682, 447334, 934603, 229712, 987946, 92988, 566149, 740521, 95230, 681258, 524672, 364619, 859449, 169050, 249181, 995686, 896201, 724987, 435495, 313409, 788431, 715077, 116061, 995997, 560378, 661230, 847588, 689374, 774471, 257427, 757728, 534584, 770753, 325748, 697718, 206907, 457749, 821390, 746620, 232285, 360547, 34459, 371689, 483421, 700848, 613139, 959275, 263365, 872401, 953320, 664825, 166108, 256638, 604327, 594894, 427502, 132598, 943304, 140816, 9055, 955019, 154408, 511398, 509149, 235664, 804977, 188141, 104309, 312017, 662736, 395359, 861512, 787042, 677730, 826387, 649331, 839569, 713519, 859545, 832150, 829438, 562187, 609347, 44706, 897760, 287072, 505109, 19393, 822486, 285776, 312995, 474820, 137251, 567681, 839110, 73070, 514603]];
}
}
60 changes: 60 additions & 0 deletions tests/SelectionSortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Src\SelectionSort;

final class SelectionSortTest extends TestCase
{
#[Test]
#[DataProvider('arrayProvider')]
public function it_should_return_the_sorted_array($arr): void
{
$this->assertEquals([1,2,3,4,5,6,7,8,9], SelectionSort::sort($arr));
}

/*
* @return array
*/
public static function arrayProvider(): array
{
return [
[[1,2,3,4,5,6,7,8,9]],
[[9,8,7,6,5,4,3,2,1]],
[[1,2,3,9,8,7,6,5,4]],
[[9,8,7,1,2,3,4,5,6]],
[[9,1,8,2,7,3,6,4,5]],
[[1,9,2,8,3,7,4,6,5]],
[[6,4,1,8,3,9,2,5,7]],
];
}

#[Test]
public function it_should_sort_single_element_array(): void
{
$this->assertEquals([4], SelectionSort::sort([4]));
}

#[Test]
public function test_empty_array(): void
{
$this->assertEquals([], SelectionSort::sort([]));
}

#[Test]
public function it_should_sort_non_consecutive_numbers_correctly(): void
{
$this->assertEquals([2,5,6,8,9], SelectionSort::sort([5,9,6,2,8]));
}

#[Test]
public function it_can_sort_array_with_1000_elements(): void
{
$random = range(1, 1000);
shuffle($random);
$this->assertEquals(range(1, 1000), SelectionSort::sort($random));
}
}
Loading