This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: heapq fails to print in sorted order for certain inputs
Type: behavior Stage: resolved
Components: Devguide Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Saimadhav.Heblikar, ezio.melotti, josh.r, wchlm
Priority: normal Keywords:

Created on 2014-04-09 08:06 by wchlm, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg215805 - (view) Author: wchlm (wchlm) Date: 2014-04-09 08:06
I observe the following in Python 2.6.1 in CentOS 5.9 and in Python 2.7.5 in MacOS 10.9.2.

A heapified 3-element array containing elements in the order [low, high, medium] fails to print in sorted order, as shown below: 

>>> import heapq
>>> lista = [1, 6, 5, 4]
>>> heapq.heapify(lista)
>>> lista # Gives sorted order, as expected
[1, 4, 5, 6]
>>> 
>>> listb = [1, 6, 5]
>>> heapq.heapify(listb)
>>> listb # Gives unsorted order -- unexpected and presumably a bug
[1, 6, 5]
>>> 
>>> [heapq.heappop(listb) for i in range(len(listb))] # But heappop works
[1, 5, 6]
msg215807 - (view) Author: Saimadhav Heblikar (Saimadhav.Heblikar) * Date: 2014-04-09 08:23
Hi,
I dont think its a bug. 
The textbook definition of a min(or max) heap is

"Heaps are binary trees for which every parent node has a value less than or equal to any of its children."

Therefore,
when lista = [1,6,5,4], and heapify is run on it,it can be viewed as
    1
  /   \
4  5  6

or [1,4,5,6]

When listb=[1,6,5], running heapify on it,gives
   1
  / \
 6   5

or [1,6,5]
heapify maintains the heap-invariant - Every key is smaller than its children. This invariant is not violated in the above example.

The heappop maintains the heap-invariant after popping. The line in your code
[heapq.heappop(listb) for i in range(len(listb))]
is the heapsort algorithm!.

Hopefully, this clears your question.
msg215808 - (view) Author: wchlm (wchlm) Date: 2014-04-09 08:34
Hi sahutd,

You may be right. I was keying off a stackoverflow example (http://stackoverflow.com/questions/12373837/heapq-module-python) that might have been misleading.

In fact, the Python documentation never does say what a printed heapified array is suppose to look like -- how it unwinds the tree structure into a linear list.

But from the above example I assumed it was supposed to print the sorted order, and again my assumption is quite possibly wrong.

It might be useful, as a documentation issue, to say what a printed heapified array is supposed to look like.
msg215848 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-04-09 21:33
It's not really relevant what a heapified list looks like (and there is no reason to guarantee a particular appearance, since it's an implementation detail that could change). It's supposed to function as a heap with the heap functions, that's all. The docs do give a guarantee that a sorted list is already "heapified", but that's a one way guarantee: All sorted lists are heaps, but not all heaps are sorted lists.

The docs also mention the big-O behavior of heapify; it's linear time, O(n), while good general purpose sorting algorithms are O(n log n). A linear algorithm cannot sort a general list.
msg215849 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-04-09 21:35
By the way, in the top answer to the Stack Overflow post you linked, it's clear that the list wasn't sorted. [44, 42, 3, 89, 10] became [3, 10, 44, 89, 42], and you'll notice, the last three elements are out of order.
msg215850 - (view) Author: wchlm (wchlm) Date: 2014-04-09 21:39
All fair enough. I see the error of my ways. I am closing the case with a Resolution of "invalid." Thanks....
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65384
2014-07-03 17:22:23ezio.melottisetstage: resolved
2014-04-09 21:39:55wchlmsetstatus: open -> closed
resolution: not a bug
messages: + msg215850
2014-04-09 21:35:37josh.rsetmessages: + msg215849
2014-04-09 21:33:42josh.rsetnosy: + josh.r
messages: + msg215848
2014-04-09 08:34:59wchlmsetmessages: + msg215808
2014-04-09 08:23:11Saimadhav.Heblikarsetnosy: + Saimadhav.Heblikar
messages: + msg215807
2014-04-09 08:06:33wchlmcreate