Connecting Tech Pros Worldwide Forums | Help | Site Map

Why doesn't my heapify work?

Dongsheng Ruan
Guest
 
Posts: n/a
#1: Feb 7 '07
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[i]>A[t]:
A[i],A[t]=A[t],A[i]



Diez B. Roggisch
Guest
 
Posts: n/a
#2: Feb 7 '07

re: Why doesn't my heapify work?


Dongsheng Ruan wrote:
Quote:
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[i]>A[t]:
A[i],A[t]=A[t],A[i]
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.htm...00000000000000

Diez
Ruan
Guest
 
Posts: n/a
#3: Feb 7 '07

re: Why doesn't my heapify work?


Can't range go from larger to smaller?


"Diez B. Roggisch" <deets@nospam.web.dewrote in message
news:52uiomF1olkevU1@mid.uni-berlin.de...
Quote:
Dongsheng Ruan wrote:
>
Quote:
>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[i]>A[t]:
> A[i],A[t]=A[t],A[i]
>
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.htm...00000000000000
>
Diez

Ruan
Guest
 
Posts: n/a
#4: Feb 7 '07

re: Why doesn't my heapify work?


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" <ruan@jcmills.comwrote in message
news:eqd4nn$473e$1@netnews.upenn.edu...
Quote:
Can't range go from larger to smaller?
>
>
"Diez B. Roggisch" <deets@nospam.web.dewrote in message
news:52uiomF1olkevU1@mid.uni-berlin.de...
Quote:
>Dongsheng Ruan wrote:
>>
Quote:
>>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[i]>A[t]:
>> A[i],A[t]=A[t],A[i]
>>
>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.htm...00000000000000
>>
>Diez
>
>

skip@pobox.com
Guest
 
Posts: n/a
#5: Feb 7 '07

re: Why doesn't my heapify work?



ruanCan'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
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
 
Posts: n/a
#6: Feb 7 '07

re: Why doesn't my heapify work?


You do know about the heapq module?
http://docs.python.org/dev/lib/module-heapq.html

--
mvh Björn
Closed Thread