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

  • SEARCH
  • Home
  • 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 6764471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:35:54+00:00 2026-05-26T14:35:54+00:00

I know how to sort-in-place an array using heapsort and max-heap property. But I

  • 0

I know how to sort-in-place an array using heapsort and max-heap property.
But I cannot think of how I could sort it in place using the min-heap property.

I can sort it using a min-heap by using a temporary intermediate array e.g.

public static int[] heapSort2(int[] array){
     buildMinHeap(array);
     int [] temp = new int[array.length];
     for(int i = 0; i < array.length; i++ ){
      temp[i] = extractMin(array);
      } 
   return temp;//returns array input sorted
}

But I can not think of a way to do it without using temp. Is it possible, or is min-heap only for priority queues?

  • 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-05-26T14:35:54+00:00Added an answer on May 26, 2026 at 2:35 pm

    Sorting in-place using a min-heap is symmetric to sorting with a max-heap.

    Suppose that you’re sorting with ascending order, and you have a max-heap array, with the root always in the first element (index 1), and you do one sorting step by moving the root out into the place of the last leaf (which is the rightmost element that is not yet sorted), making this leaf the new root and sinking it into the heap.

    Then you sort with ascending order using a min-heap array by having the root always in the last element (index N), and you do one sorting step by moving the root out into the place of the last leaf (which is the leftmost element that is not yet sorted), making this leaf the new root and sinking it into the heap.

    The equations for getting the indices of the parent, left and right children are different in such a heap. In the former case, assuming indices 1->N, the equations are:

    parent(i) = floor(i / 2)
    leftchild(i) = 2 * i
    rightchild(i) = 2 * i + 1
    

    In the latter, reversed min-heap case, the equations are obtained by reversing the indices at input and output of the above:

    parent(i) = N + 1 - floor((N + 1 - i) / 2)
    leftchild(i) = N + 1 - (2 * (N + 1 - i))
    rightchild(i) = N + 1 - (2 * (N + 1 - i) + 1)
    

    You can see that the latter equations look ugly compared to the former (but it may be possible to simplify them). Therefore, when sorting in ascending order, you’re best off only using a max-heap, not a min-heap. Use a min-heap only when you want to sort in descending order – in the same orientation as you would use a max-heap when sorting in ascending order (root on first element).

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

Sidebar

Related Questions

I know that JTable can sort by a single column. But is it possible
I know this sort of code is not best practice, but nevertheless in certain
(I know this is a sort of lazy question, but it's oddly hard to
I know VS2008 has the remove and sort function for cleaning up using directives,
I know I can use something like User.sort {|a, b| a.attribute <=> b.attribute} or
This is sort of not relevant to programming but I think it also is:
Possible Duplicate: Sort strings and numbers in Ruby I have an array of place
I know there are natsort() and natcasesort() functions in php to sort array elements
does anyone know any sort of app that lets users visit a page on
Anyone know of a Linux/D-Bus sort of mechanism for Windows? Thanks

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.