Реализации алгоритмов/Сортировка/Пирамидальная: различия между версиями

Содержимое удалено Содержимое добавлено
Perl
Строка 1:
{{wikipedia|Пирамидальная сортировка}}
 
== [[w:Си_(язык_программирования)|C]] ==
== C++ ==
<big><source lang="c">
#include <stdio.h>
 
#define MAXL 1000
 
void swap (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int main()
{
int a[MAXL], n, i, sh = 0, b = 0;
scanf ("%i", &n);
for (i = 0; i < n; ++i)
scanf ("%i", &a[i]);
while (1)
{
b = 0;
for (i = 0; i < n; ++i)
{
if (i*2 + 2 + sh < n)
{
if (a[i+sh] > a[i*2 + 1 + sh] || a[i + sh] > a[i*2 + 2 + sh])
{
if (a[i*2+1+sh] < a[i*2+2+sh])
{
swap (&a[i+sh], &a[i*2+1+sh]);
b = 1;
}
else if (a[i*2+2+sh] < a[i*2+1+sh])
{
swap (&a[i+sh],&a[i*2+2+sh]);
b = 1;
}
}
}
else if (i * 2 + 1 + sh < n)
{
if (a[i+sh] > a[i*2+1+sh])
{
swap (&a[i+sh], &a[i*2+1+sh]);
b=1;
}
}
}
if (!b) sh++;
if (sh+2==n)
break;
}
for (i = 0; i < n; ++i)
printf ("%i%c", a[i], (i!=n-1)?' ':'\n');
return 0;
}
</source></big>
 
== [[w:C++|C++]] ==
<source lang="cpp">
<big><source lang="cpp">
#include <iterator>
 
Строка 55 ⟶ 112 :
pop_heap( first, last-- );
}
</source></big>
 
== C++ (другой вариант) ==
<big><source lang="cpp">
#include <iostream>
#include <conio.h>
Строка 133 ⟶ 190 :
return 0;
}
</source></big>
 
==[[w:C_Sharp|C#]]==
<big><source lang="csharp">
static Int32 add2pyramid(Int32[] arr, Int32 i, Int32 N)
{
Строка 208 ⟶ 265 :
System.Console.ReadLine();
}
</source></big>
 
== C# (другой вариант) ==
<big><source lang="csharp">
public class Heap<T>
{
Строка 285 ⟶ 342 :
}
 
</source> </big>
Здесь T - любой тип, на множестве элементов которого можно ввести [[w:Частично упорядоченное множество|отношение частичного порядка]].
 
== [[w:Pascal|Pascal]] ==
 
Вместо '''«SomeType»''' следует подставить любой из арифметических типов (например [[integer]]).
 
<big><source lang="pascal">
 
 
procedure Sort(var Arr: array of SomeType; Count: Integer);
 
Строка 332 ⟶ 387 :
end;
end;
</source></big>
 
== Pascal (другой вариант) ==
Примечание:
Строка 338 ⟶ 394 :
N — количество элементов массива
 
<big><source lang="pascal">
procedure HeapSort(var m: myarray; N: integer);
var
Строка 393 ⟶ 449 :
end;
end;
</source></big>
 
 
== Pascal (третий вариант) ==
<big><source lang="pascal">
//процедура для перессылки записей
procedure swap(var x,y:integer);
Строка 434 ⟶ 490 :
left(a,n);
end;
</source></big>
 
== [[w:Python|Python]] ==
<big><source lang="python">
 
<source lang="python">
def heapSort(li):
"""Сортирует список в возрастающем порядке с помощью алгоритма пирамидальной сортировки"""
Строка 462 ⟶ 517 :
li[0] = temp
downHeap(li, 0, i)
</source></big>
 
== C[[w:Perl|Perl]] ==
<big><source lang="perl">
$N=@out+0;
 
if ($N>1) #Любой массив из одного символа уже отсортирован
<source lang="c">
#include <stdio.h>
 
#define MAXL 1000
 
void swap (int *a, int *b)
{
while ($sh+2!=$N)
int t = *a;
*a = *b; {
*b = t $b=undef;
for ($i=0; $i<$N; $i++)
}
{
int main()
if ($i*2+2+$sh<$N)
{
{
int a[MAXL], n, i, sh = 0, b = 0;
if ( ($out[$i+$sh] gt $out[$i*2+1+$sh] ) || ($out[$i+$sh] gt $out[$i*2+2+$sh]) )
scanf ("%i", &n);
{
for (i = 0; i < n; ++i)
scanfif ("%$out[$i",*2+1+$sh] &alt $out[$i*2+2+$sh]);
{
while (1)
($out[$i*2+1+$sh], $out[$i+$sh]) = ($out[$i+$sh], $out[$i*2+1+$sh]);
{
$b = 01;
}
for (i = 0; i < n; ++i)
elsif ($out[$i*2+2+$sh] lt $out[$i*2+1+$sh])
{
{
if (i*2 + 2 + sh < n)
($out[$i*2+2+$sh], $out[$i+$sh]) = ($out[$i+$sh], $out[$i*2+2+$sh]);
{
$b=1;
if (a[i+sh] > a[i*2 + 1 + sh] || a[i + sh] > a[i*2 + 2 + sh])
}
{
}
if (a[i*2+1+sh] < a[i*2+2+sh])
if ($out[$i*2+2+$sh] lt $out[$i*2+1+$sh])
{
{
swap (&a[i+sh], &a[i*2+1+sh]);
($out[$i*2+1+$sh], $out[$i*2+2+$sh]) = ($out[$i*2+2+$sh], $out[$i*2+1+$sh]);
b = 1;
} $b=1;
}
else if (a[i*2+2+sh] < a[i*2+1+sh])
{ }
swapelsif (&a[i+sh],&a[$i*2+21+$sh] < $N);
{
b = 1;
if ($out[$i+$sh] gt $out[$i*2+1+$sh])
}
{
($out[$i+$sh], $out[$i*2+1+$sh]) = ($out[$i*2+1+$sh], $out[$i+$sh]);
$b=1;
}
}
}
if (!$b) {$sh++;}
if ($sh+2 == $N) {last;}
}
}
else if (i * 2 + 1 + sh < n)
{
if (a[i+sh] > a[i*2+1+sh])
{
swap (&a[i+sh], &a[i*2+1+sh]);
b=1;
}
}
}
if (!b) sh++;
if (sh+2==n)
break;
}
for (i = 0; i < n; ++i)
printf ("%i%c", a[i], (i!=n-1)?' ':'\n');
return 0;
}
</source></big>
 
 
</source>