I have an array arr = [1,2,1,3].
When I use heappush function to push the array elements as they are into a heap, the order of stays exactly the same as it is in the array:
arr = [1,2,1,3]
heap = []
for i in range(len(arr)):
heapq.heappush(heap, arr[i])
print(heap)
Result: [1, 2, 1, 3]
Now, if I push the negative values of the elements of arr, the heap becomes sorted as it suppoed to be.
arr = [1,2,1,3]
heap = []
for i in range(len(arr)):
heapq.heappush(heap, - arr[i]) <---- Here
print(heap)
Result: [-3, -2, -1, -1]
I want to know why heappush sorts the array elements when their negative values are added to the heap but it does not do anything when the positive values are pushed into the heap.