Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 2,544: | Line 2,544: | ||
element 25 after sort zeta |
element 25 after sort zeta |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
|||
{{works with|PowerShell|4.0}} |
|||
<lang PowerShell> |
|||
function heapsort($a, $count) { |
|||
$a = heapify $a $count |
|||
$end = $count - 1 |
|||
while( $end -gt 0) { |
|||
$temp = $a[$end] |
|||
$a[$end] = $a[0] |
|||
$a[0] = $temp |
|||
$end-- |
|||
$a = siftDown $a 0 $end |
|||
} |
|||
$a |
|||
} |
|||
function heapify($a, $count) { |
|||
$start = [Math]::Floor(($count - 2) / 2) |
|||
while($start -ge 0) { |
|||
$a = siftDown $a $start ($count-1) |
|||
$start-- |
|||
} |
|||
$a |
|||
} |
|||
function siftdown($a, $start, $end) { |
|||
$b, $root = $true, $start |
|||
while(( ($root * 2 + 1) -le $end) -and $b) { |
|||
$child = $root * 2 + 1 |
|||
if( ($child + 1 -le $end) -and ($a[$child] -lt $a[$child + 1]) ) { |
|||
$child++ |
|||
} |
|||
if($a[$root] -lt $a[$child]) { |
|||
$temp = $a[$root] |
|||
$a[$root] = $a[$child] |
|||
$a[$child] = $temp |
|||
$root = $child |
|||
} |
|||
else { $b = $false} |
|||
} |
|||
$a |
|||
} |
|||
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11) |
|||
"$(heapsort $array $array.Count)" |
|||
</lang> |
|||
<b>Output:</b> |
|||
<pre> |
|||
3 8 11 19 21 36 60 63 80 87 100 |
|||
</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<lang PureBasic>Declare heapify(Array a(1), count) |
<lang PureBasic>Declare heapify(Array a(1), count) |