Реализации алгоритмов/Сортировка/Шелла: различия между версиями
Содержимое удалено Содержимое добавлено
→Perl: Программа приведена к человекочитаемому виду Метки: с мобильного устройства из мобильной версии |
|||
Строка 309:
== [[w:Perl|Perl]] ==
<source lang="
use warnings;
@out=(5,3,7,9,2,1,6,5,3,7,9,3,4);▼
use strict;
for(my$k=int($N/2);$k>0;$k=int($k/2)){▼
use feature 'say';
say "@array - initial";
$j--;▼
# Do a gapped insertion sort for this gap size.
# The first gap elements a[0..gap-1] are already in gapped order
# keep adding one more element until the entire array is gap sorted
for (my $i = $k; $i < @array; ++$i) {
# add a[i] to the elements that have been gap sorted
# save a[i] in temp and make a hole at position i
my $temp = $array[$i];
# shift earlier gap-sorted elements up until the correct location for a[i] is found
for ($j = $i; $j >= $k && $array[$j - $k] > $temp; $j -= $k) {
$array[$j] = $array[$j - $k];
}
# put temp (the original a[i]) in its correct location
$array[$j] = $temp;
}
say "@array";
}
</source>▼
#5 3 7 9 2 1 6 5 3 7 9 3 4 - initial
#4 3 3 7 2 1 5 5 7 9 9 3 6
#4 2 1 5 3 3 6 5 3 7 9 7 9
#1 2 3 3 3 4 5 5 6 7 7 9 9
▲</source>
== [[w:PL/SQL|PLSQL]] ==
|