Why doesn't my heapify work?

D

Dongsheng Ruan

I want to turn an Array into a heap, but my code just doesn't work: no
change after execution.

A=[3,5,4,9,6,7]
m=len(A)-1



for i in range(m,1):
t=(i-1)/2
if A>A[t]:
A,A[t]=A[t],A
 
D

Diez B. Roggisch

Dongsheng said:
I want to turn an Array into a heap, but my code just doesn't work: no
change after execution.

A=[3,5,4,9,6,7]
m=len(A)-1



for i in range(m,1):
t=(i-1)/2
if A>A[t]:
A,A[t]=A[t],A


First of all, there is the module heapq that will just do it.

And then you seem to misunderstand how the range-function works. range(m, 1)
will always be the empty list. See

pydoc range

for how it operates.

Overall, your code is very unpythonic, to say the least. I suggest you start
reading the python tutorial first:

http://docs.python.org/tut/

Especially the looping techniques section:

http://docs.python.org/tut/node7.html#SECTION007600000000000000000

Diez
 
R

Ruan

Can't range go from larger to smaller?


Diez B. Roggisch said:
Dongsheng said:
I want to turn an Array into a heap, but my code just doesn't work: no
change after execution.

A=[3,5,4,9,6,7]
m=len(A)-1



for i in range(m,1):
t=(i-1)/2
if A>A[t]:
A,A[t]=A[t],A


First of all, there is the module heapq that will just do it.

And then you seem to misunderstand how the range-function works. range(m,
1)
will always be the empty list. See

pydoc range

for how it operates.

Overall, your code is very unpythonic, to say the least. I suggest you
start
reading the python tutorial first:

http://docs.python.org/tut/

Especially the looping techniques section:

http://docs.python.org/tut/node7.html#SECTION007600000000000000000

Diez
 
R

Ruan

I found out what is wrong.

You must give it a negative step, like range(10,1,-1)

But my code is not good enought for heapify.

I will try again.


Ruan said:
Can't range go from larger to smaller?


Diez B. Roggisch said:
Dongsheng said:
I want to turn an Array into a heap, but my code just doesn't work: no
change after execution.

A=[3,5,4,9,6,7]
m=len(A)-1



for i in range(m,1):
t=(i-1)/2
if A>A[t]:
A,A[t]=A[t],A


First of all, there is the module heapq that will just do it.

And then you seem to misunderstand how the range-function works. range(m,
1)
will always be the empty list. See

pydoc range

for how it operates.

Overall, your code is very unpythonic, to say the least. I suggest you
start
reading the python tutorial first:

http://docs.python.org/tut/

Especially the looping techniques section:

http://docs.python.org/tut/node7.html#SECTION007600000000000000000

Diez

 
S

skip

ruan> Can't range go from larger to smaller?

Yes. Read the doc:

range(...)
range([start,] stop[, step]) -> list of integers

Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.

To go from larger to smaller you need a negative step size. Also, note that
the endpoint is never included. range(10) serves up 0 throu 9.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top