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 4014938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:33:28+00:00 2026-05-20T09:33:28+00:00

I wonder what’s the algorithm of make_heap in in C++ such that the complexity

  • 0

I wonder what’s the algorithm of make_heap in in C++ such that the complexity is 3*N? Only way I can think of to make a heap by inserting elements have complexity of O(N Log N). Thanks a lot!

  • 1 1 Answer
  • 1 View
  • 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-05-20T09:33:28+00:00Added an answer on May 20, 2026 at 9:33 am

    You represent the heap as an array. The two elements below the i‘th element are at positions 2i+1 and 2i+2. If the array has n elements then, starting from the end, take each element, and let it "fall" to the right place in the heap. This is O(n) to run.

    Why? Well for n/2 of the elements there are no children. For n/4 there is a subtree of height 1. For n/8 there is a subtree of height 2. For n/16 a subtree of height 3. And so on. So we get the series n/22 + 2n/23 + 3n/24 + ... = (n/2)(1 * (1/2 + 1/4 + 1/8 + . ...) + (1/2) * (1/2 + 1/4 + 1/8 + . ...) + (1/4) * (1/2 + 1/4 + 1/8 + . ...) + ...) = (n/2) * (1 * 1 + (1/2) * 1 + (1/4) * 1 + ...) = (n/2) * 2 = n. Or, formatted maybe more readably to see the geometric series that are being summed:

    n/2^2 + 2n/2^3 + 3n/2^4 + ...
      = (n/2^2 +  n/2^3 +  n/2^4 + ...)
               + (n/2^3 +  n/2^4 + ...)
                        + (n/2^4 + ...)
        + ...
      = n/2^2 (1 + 1/2 + 1/2^4 + ...)
               + n/2^3 (1 + 1/2 + 1/2^3 + ...)
                        + n/2^4 (1 + 1/2 + 1/2^3 + ...)
        + ...
      = n/2^2 * 2
               + n/2^3 * 2
                        + n/2^4 * 2
        + ...
      = n/2 + n/2^2 + n/2^3 + ...
      = n(1/2 + 1/4 + 1/8 + ...)
      = n
    

    And the trick we used repeatedly is that we can sum the geometric series with

    1 + 1/2 + 1/4 + 1/8 + ...
       = (1 + 1/2 + 1/4 + 1/8 + ...) (1 - 1/2)/(1 - 1/2)
       = (1 * (1 - 1/2)
            + 1/2 * (1 - 1/2)
                  + 1/4 * (1 - 1/2)
                        + 1/8 * (1 - 1/2)
                              + ...) / (1 - 1/2)
       = (1 - 1/2
            + 1/2 - 1/4
                  + 1/4 - 1/8
                        + 1/8 - 1/16
                              + ...) / (1 - 1/2)
       = 1 / (1 - 1/2)
       = 1 / (1/2)
       = 2
    

    So the total number of "see if I need to fall one more, and if so which way do I fall? comparisons comes to n. But you get round-off from discretization, so you always come out to less than n sets of swaps to figure out. Each of which requires at most 3 comparisons. (Compare root to each child to see if it needs to fall, then the children to each other if the root was larger than both children.)

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

Sidebar

Related Questions

I wonder about that can I write native SQL to add or delete operations
I wonder how to mark a specific test in eunit in a way that
Wonder if anyone can help, I'm looking for a way to take an image
I wonder how I can write the jQuery is method using only javascript? You
Wonder if anyone can help. I have a c# web application that uses the
wonder whether someone can help me with the following one... I have a struct
wonder if someone can help me i know what i want to achive but
I wonder what is the best way to measure the execution time of some
I wonder if there is a way for me to SCP the file from
I wonder if anyone can help - I want to select a table and

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.