Skip to content

Instantly share code, notes, and snippets.

@jhorikawa
Created October 24, 2020 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhorikawa/ca813108767389776e3e658ac180fa30 to your computer and use it in GitHub Desktop.
Save jhorikawa/ca813108767389776e3e658ac180fa30 to your computer and use it in GitHub Desktop.
import random
vals = []
for i in range(7):
vals.append(random.random() * 100)
heap = []
for i in range(len(vals)):
val = vals[i]
l = len(heap)-1
heap.append(val)
while(True):
pindex = l // 2
if pindex < 0 or pindex >= len(heap):
break
val = heap[l+1]
pval = heap[pindex]
if pval < val:
heap[pindex] = val
heap[l+1] = pval
l = pindex - 1
if pindex < 0:
break
else:
break
print('___array___')
print(vals)
print('___heap___')
print(heap)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment