Why doesn't my heapify work?

Ruan ruan at jcmills.com
Wed Feb 7 13:17:59 EST 2007


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 at jcmills.com> wrote in message 
news:eqd4nn$473e$1 at netnews.upenn.edu...
> Can't range go from larger to smaller?
>
>
> "Diez B. Roggisch" <deets at nospam.web.de> wrote in message 
> news:52uiomF1olkevU1 at mid.uni-berlin.de...
>> Dongsheng Ruan wrote:
>>
>>> 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.html#SECTION007600000000000000000
>>
>> Diez
>
> 





More information about the Python-list mailing list