Sorting algorithms/Heapsort: Difference between revisions

Replace translated implementation with native implementation of pseudocode from the introduction
(Add PL/M)
(Replace translated implementation with native implementation of pseudocode from the introduction)
Line 2,718:
 
=={{header|Javascript}}==
{{trans|CoffeeScript}}
<lang Javascript>
function swapheapSort(data, i, jarr) {
heapify(arr)
var tmp = data[i];
data[i]end = data[j];arr.length - 1
data[j]while =(end tmp;> 0) {
[arr[end], arr[0]] = [arr[0], arr[end]]
}
end -= 1-
 
swapsiftDown(arr, 0, end);
function heap_sort(arr) {
put_array_in_heap_order(arr);
var end = arr.length - 1;
while(end > 0) {
swap(arr, 0, end);
sift_element_down_heap(arr, 0, end);
end -= 1
}
}
 
function put_array_in_heap_orderheapify(arr) {
var endstart = Math.floor(arr.length/2) - 1;
var i;
 
i = arr.length / 2 - 1;
iwhile (start >= Math.floor(i0); {
sift_element_down_heapsiftDown(arr, istart, arr.length - 1);
while (i >= 0) {
i start--= 1;
sift_element_down_heap(arr, i, arr.length);
i -= 1;
}
}
 
function sift_element_down_heapsiftDown(heaparr, istartPos, maxendPos) {
varlet i_big,rootPos c1,= c2;startPos
 
while(i < max) {
while (rootPos * 2 i_big+ 1 <= i;endPos) {
c1childPos = 2rootPos *i 2 + 1;
c2if (childPos + 1 <= c1endPos && arr[childPos] < arr[childPos + 1;]) {
if (c1 < max && heap[c1] > heap[i_big])childPos++
i_big = c1;}
if (c2 < max && heaparr[c2rootPos] >< heaparr[i_bigchildPos]) {
i_big[arr[rootPos], arr[childPos]] = c2;[arr[childPos], arr[rootPos]]
if (i_big == i) return;rootPos = childPos
swap(heap,i,} i_big);else {
i = i_big; return
var i; }
}
}
test('rosettacode', () => {
 
arr = [12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8,];
heap_sort heapSort(arr);
expect(arr).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
alert(arr});</lang>
{{out}}
<pre>