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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:38:34+00:00 2026-05-28T06:38:34+00:00

At school we are currently learning sorting algorithms in Java and I got for

  • 0

At school we are currently learning sorting algorithms in Java and I got for my homework the Heap Sort. I did my reading, I tried to find out as much as I could, but it seems I just can’t grasp the concept.

I’m not asking you to write me a Java program, if you could just explain to me as simply as you can how the Heap Sort works.

  • 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-28T06:38:35+00:00Added an answer on May 28, 2026 at 6:38 am

    Right, so basically you take a heap and pull out the first node in the heap – as the first node is guaranteed to be the largest / smallest depending on the direction of sort. The tricky thing is re-balancing / creating the heap in the first place.

    Two steps were required for me to understand the heap process – first of all thinking of this as a tree, getting my head around it, then turning that tree into an array so it could be useful.

    The second part of that is to essentially traverse the tree breadth first, left to right adding each element into the array. So the following tree:

                                        73                          
                                     7      12          
                                   2   4  9   10    
                                 1          
    

    Would be {73,7,12,2,4,9,10,1}

    The first part requires two steps:

    1. Make sure each node has two children (Unless you don’t have enough nodes to do that as in the tree above.
    2. Make sure each node is bigger (Or smaller if sorting min first) than its children.

    So to heapify a list of numbers you add each one to the heap, then following those two steps in order.

    To create my heap above I will add 10 first – it’s the only node so nothing to do.
    Add 12 as it’s child on the left:

        10
      12
    

    This satisfies 1, but not 2 so I will swap them round:

        12
      10
    

    Add 7 – nothing to do

        12
      10  7
    

    Add 73

              12
           10     7
        73
    

    10 < 73 so need to swap those:

              12
           73     7
        10
    

    12 < 73 so need to swap those:

              73
           12     7
        10
    

    Add 2 – nothing to do

              73
           12     7
        10   2
    

    Add 4 – nothing to do

              73
           12     7
        10   2  4
    

    Add 9

              73
           12     7
        10   2  4   9
    

    7 < 9 – swap

              73
           12     9
        10   2  4   7
    

    Add 1 – nothing to do

              73
           12     9
        10   2  4   7
      1
    

    We have our heap 😀

    Now you just remove each element from the top, swapping in the last element to the top of the tree each time, then re-balancing the tree:

    Take 73 off – putting 1 in its place

              1
           12     9
        10   2  4   7
    

    1 < 12 – so swap them

              12
            1    9
        10   2  4   7
    

    1 < 10 – so swap them

              12
           10     9
         1   2  4   7
    

    Take 12 off – replace with 7

              7
           10     9
         1   2  4   
    

    7 < 10 – swap them

              10
           7     9
         1   2  4   
    

    Take 10 off – replace with 4

              4
           7     9
        1   2  
    

    4 < 7 – swap

              7
           4     9
        1   2  
    

    7 < 9 – swap

              9
           4     7
        1   2 
    

    Take 9 off – replace with 2

              2
           4     7
        1   
    

    2 < 4 – swap them

              4
           2     7
        1  
    

    4 < 7 – swap them

              7
           2     4
        1  
    

    Take 7 off – replace with 1

              1
           2     4
    

    1 < 4 – swap them

              4
           2     1
    

    Take 4 – replace with 1

              1
           2
    

    1 < 2 – swap them

              2
           1
    

    Take 2 – replace with 1

              1
    

    Take 1

    Sorted list voila.

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

Sidebar

Related Questions

I am learning C++ in school now. Currently using C++ with codeblocks on my
I'm currently running a Django project on my school's webserver with FCGI. I did
I went to school for programming years ago and when I got out I
I am currently working on a project for school that is a java memo
I am currently in the process of making a java minesweeper game for school
I currently have a school assignment that involves both PHP and asp.net. Now the
I currently have a download site for my school that is based in .net.
I'm currently designing a brand new database. In school, we always learned to put
I'm currently working on a school project in Eclipse (We have just started using
I'm currently a senior in high school, about to matriculate and pursue a major

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.