Skip to content

Commit 8242d65

Browse files
committed
Implement sort in PHP
refs Al2Klimov#2
1 parent 64566db commit 8242d65

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

sort.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
function checkForNextLine($arr)
4+
{
5+
$length = sizeof($arr);
6+
7+
for ($i = 0; $i < $length; $i++) {
8+
if (strpos($arr[$i], "\n") === false) {
9+
$arr[$i] .= "\n";
10+
}
11+
}
12+
13+
return $arr;
14+
}
15+
16+
function bubbleSort($sort_array)
17+
{
18+
$array_indexes = count($sort_array);
19+
$sorted = false;
20+
21+
for ($i = 0; $i < $array_indexes; $i++) {
22+
for ($j = 0; $j < $array_indexes - $i - 1; $j++) {
23+
if ($sort_array[$j + 1] < $sort_array[$j]) {
24+
$tmp = $sort_array[$j + 1];
25+
$sort_array[$j + 1] = $sort_array[$j];
26+
$sort_array[$j] = $tmp;
27+
28+
$sorted = true;
29+
}
30+
}
31+
32+
if (! $sorted) {
33+
break;
34+
}
35+
}
36+
37+
return $sort_array;
38+
}
39+
40+
echo implode("", bubbleSort(checkForNextLine(file('php://stdin'))));

0 commit comments

Comments
 (0)