Zadanie 8.
Stosując algorytm sortowania metodą Shella, posortuj tablicę $data
w porządku rosnącym.$dane = Array
(
[0] => 130
[1] => 4
[2] => 12
[3] => 32
[4] => 40
[5] => 3
[6] => 14
[7] => 19
[8] => 27
[9] => 25
[10] => 31
[11] => 49
[12] => 51
[13] => 55
[14] => 50
[15] => 100
)
function shell_sort($arr) {
$x = round(count($arr)/2);
while($x > 0) {
for($i = $x; $i < count($arr);$i++){
$temp = $arr[$i];
$j = $i;
while($j >= $x && $arr[$j-$x] > $temp) {
$arr[$j] = $arr[$j - $x];
$j -= $x;
}
$arr[$j] = $temp;
}
$x = round($x/2.2);
}
return $arr;
}
$dane = shell_sort($dane);
echo "Posortowana tablica:";
echo "<pre>\$dane = ";
print_r($dane);
echo "</pre><hr>";
Posortowana tablica:$dane = Array
(
[0] => 3
[1] => 4
[2] => 12
[3] => 14
[4] => 19
[5] => 25
[6] => 27
[7] => 31
[8] => 32
[9] => 40
[10] => 49
[11] => 50
[12] => 51
[13] => 55
[14] => 100
[15] => 130
)
Złożoność obliczeniowa algorytmu O(n⋅(log n)²).