Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,43 @@
|
|||
fn heapify arr count =
|
||||
(
|
||||
local s = count /2
|
||||
while s > 0 do
|
||||
(
|
||||
arr = siftDown arr s count
|
||||
s -= 1
|
||||
)
|
||||
return arr
|
||||
)
|
||||
fn siftDown arr s end =
|
||||
(
|
||||
local root = s
|
||||
while root * 2 <= end do
|
||||
(
|
||||
local child = root * 2
|
||||
if child < end and arr[child] < arr[child+1] do
|
||||
(
|
||||
child += 1
|
||||
)
|
||||
if arr[root] < arr[child] then
|
||||
(
|
||||
swap arr[root] arr[child]
|
||||
root = child
|
||||
)
|
||||
else return arr
|
||||
)
|
||||
return arr
|
||||
)
|
||||
fn heapSort arr =
|
||||
(
|
||||
local count = arr.count
|
||||
arr = heapify arr count
|
||||
local end = count
|
||||
while end >= 1 do
|
||||
(
|
||||
swap arr[1] arr[end]
|
||||
|
||||
end -= 1
|
||||
arr = siftDown arr 1 end
|
||||
)
|
||||
|
||||
)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a = for i in 1 to 10 collect random 0 9
|
||||
#(7, 2, 5, 6, 1, 5, 4, 0, 1, 6)
|
||||
heapSort a
|
||||
#(0, 1, 1, 2, 4, 5, 5, 6, 6, 7)
|
||||
Loading…
Add table
Add a link
Reference in a new issue