Created
October 24, 2020 19:59
-
-
Save jhorikawa/ca813108767389776e3e658ac180fa30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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