This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Heap sort. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Heap sort." Watch "Heap sort." New topic

Heap sort.

Munna Takedo

Joined: May 13, 2010
Posts: 1
Can anyone explain me the Heap sort logic and how to implement it in java ?
And suggest me which sort algorithm is the best in performance wise with huge Data and less Data?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Heapsort. There is an example there in Pascal / Delphi. Let's see if you can read that. For your information, ":=" is the assignment operator in Pascal / Delphi.

How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

the Heap data structure is basically a tree, where the child nodes are always smaller (by whatever definition of 'smaller' you choose) than the parent.

There are algorithms for re-heapifying a heap once you remove an element.

A Heap sort builds a heap out of your data.

you remove the largest node (which is by definition the root of the heap), and stick it at the end of your array, then re-heapify what's left.

You then remove the largest node, put it in the second to last spot, and re-heapify.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

Heap is a complete binary tree.

Good luck!!
A small leak can sink a Gigantic ship.>
I agree. Here's the link:
subject: Heap sort.
It's not a secret anymore!