Reputation: 51
What does the -1, -1 in range(n, -1, -1) do, I have encountered this same pattern and implementation with various sorting algo, and it's confusing me so much. Any answers would be appreciated! thank you in advance - NewCoder
-SAMPLE CODE-
def build_max_heap(A):
n = len(A)
for i in range(n, -1,-1):
max_heapify(A,n, i)
for i in range(n-1,0,-1):
A[0],A[i]=A[i],A[0]
max_heapify(A,i,0)
A=[33,35,42,10,7,8,14,19,48]
build_max_heap(A)
print(A)
Upvotes: 0
Views: 59
Reputation: 146
The signature for range
is range(start, stop[, step])
.
The range
function in range(n, -1, -1)
accept 3 argument:
'n' as the start index of the range object
the first '-1' as the end index of the range object
the second '-1' as the move step of the range object
So range(n, -1, -1)
basic means construct a sequence of [n, n-1, ..., 1, 0].
For more you can refer to Python Doc
Upvotes: 1