June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,36 @@
|
|||
swap := proc(arr, a, b)
|
||||
local temp:
|
||||
temp := arr[a]:
|
||||
arr[a] := arr[b]:
|
||||
arr[b] := temp:
|
||||
end proc:
|
||||
heapify := proc(toSort, n, i)
|
||||
local largest, l, r, holder:
|
||||
largest := i:
|
||||
l := 2*i:
|
||||
r := 2*i+1:
|
||||
if (l <= n and toSort[l] > toSort[largest]) then
|
||||
largest := l:
|
||||
end if:
|
||||
if (r <= n and toSort[r] > toSort[largest]) then
|
||||
largest := r:
|
||||
end if:
|
||||
if (not largest = i) then
|
||||
swap(toSort, i, largest);
|
||||
heapify(toSort, n, largest):
|
||||
end if:
|
||||
end proc:
|
||||
heapsort := proc(arr)
|
||||
local n,i:
|
||||
n := numelems(arr):
|
||||
for i from trunc(n/2) to 1 by -1 do
|
||||
heapify(arr, n, i):
|
||||
end do:
|
||||
for i from n to 2 by -1 do
|
||||
swap(arr, 1, i):
|
||||
heapify(arr, i-1, 1):
|
||||
end do:
|
||||
end proc:
|
||||
arr := Array([17,3,72,0,36,2,3,8,40,0]);
|
||||
heapsort(arr);
|
||||
arr;
|
||||
Loading…
Add table
Add a link
Reference in a new issue