Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:43:57+00:00 2026-06-15T17:43:57+00:00

Reading Guido’s infamous answer to the question Sorting a million 32-bit integers in 2MB

  • 0

Reading Guido’s infamous answer to the question Sorting a million 32-bit integers in 2MB of RAM using Python, I discovered the module heapq.

I also discover I didn’t understand jack about it, nor did I know what I could do with it.

Can you explain to me (with the proverbial 6 years old target) what is the heap queue algorithm for and what you can do with it ?

Can you provide a simple Python snippet where using it (with the heapq module) solves a problem that will be better solved with it and not with something else ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:43:57+00:00Added an answer on June 15, 2026 at 5:43 pm

    heapq implements binary heaps, which are a partially sorted data structure. In particular, they have three interesting operations:

    • heapify turns a list into a heap, in-place, in O(n) time;
    • heappush adds an element to the heap in O(lg n) time;
    • heappop retrieves the smallest element off the heap in O(lg n) time.

    Many interesting algorithms rely on heaps for performance. The simplest one is probably partial sorting: getting the k smallest (or largest) elements of a list without sorting the entire list. heapq.nsmallest (nlargest) does that. The implementation of nlargest can be paraphrased as:

    def nlargest(n, l):
        # make a heap of the first n elements
        heap = l[:n]
        heapify(heap)
    
        # loop over the other len(l)-n elements of l
        for i in xrange(n, len(l)):
            # push the current element onto the heap, so its size becomes n+1
            heappush(heap, l[i])
            # pop the smallest element off, so that the heap will contain
            # the largest n elements of l seen so far
            heappop(heap)
    
        return sorted(heap, reverse=True)
    

    Analysis: let N be the number of elements in l. heapify is run once, for a cost of O(n); that’s negligible. Then, in a loop running N-n = O(N) times, we perform a heappop and a heappush at O(lg n) cost each, giving a total running time of O(N lg n). When N >> n, this is a big win compared to the other obvious algorithm, sorted(l)[:n], which takes O(N lg N) time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading Guido's Sorting a million 32-bit integers in 2MB of RAM using Python
I am now reading the guide to building ontology using Protege tutorial that deals
I'm reading this google guide and using this sample code provided by google ,
I am using the csv module for Python. I have had a good look
I'm thinking about using CodeIgniter for a new project. I was reading the user-guide
Disclaimer: i am new to python but have drupal programming experience I am reading
I've been reading through Beej's Guide to Network Programming to get a handle on
I have been reading the JavaScript Scripting Guide for Quicktime and in there it
I've been reading O'Reilly book Dojo - The Definitive Guid but somethings are still
I was reading Joshua Bloch's Effective Java Programming Language Guide . He explains static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.