September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
51
Task/Priority-queue/Phix/priority-queue-2.phix
Normal file
51
Task/Priority-queue/Phix/priority-queue-2.phix
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
sequence pq = {}
|
||||
|
||||
constant PRIORITY = 2
|
||||
|
||||
procedure pq_add(sequence item)
|
||||
-- item is {object data, object priority}
|
||||
integer n = length(pq)+1,
|
||||
m = floor(n/2)
|
||||
pq &= 0
|
||||
-- append at end, then up heap
|
||||
while m>0 and item[PRIORITY]<pq[m][PRIORITY] do
|
||||
pq[n] = pq[m]
|
||||
n = m
|
||||
m = floor(m/2)
|
||||
end while
|
||||
pq[n] = item
|
||||
end procedure
|
||||
|
||||
function pq_pop()
|
||||
sequence result = pq[1]
|
||||
|
||||
integer qn = length(pq),
|
||||
n = 1,
|
||||
m = 2
|
||||
while m<qn do
|
||||
if m+1<qn and pq[m][PRIORITY]>pq[m+1][PRIORITY] then
|
||||
m += 1
|
||||
end if
|
||||
if pq[qn][PRIORITY]<=pq[m][PRIORITY] then exit end if
|
||||
pq[n] = pq[m]
|
||||
n = m
|
||||
m = m * 2
|
||||
end while
|
||||
pq[n] = pq[qn]
|
||||
pq = pq[1..$-1]
|
||||
return result
|
||||
end function
|
||||
|
||||
constant set = shuffle({{"Clear drains", 3},
|
||||
{"Feed cat", 4},
|
||||
{"Make tea", 5},
|
||||
{"Solve RC tasks", 1},
|
||||
{"Tax return", 2}})
|
||||
for i=1 to length(set) do
|
||||
pq_add(set[i])
|
||||
pq_add(set[rand(length(set))])
|
||||
end for
|
||||
|
||||
while length(pq) do
|
||||
?pq_pop()
|
||||
end while
|
||||
Loading…
Add table
Add a link
Reference in a new issue