×

Heap Sort in Java

In this section, we have discussed the max-heap for sorting the elements.

Max Heap Algorithm

Step 1: Build max heap for the given input array using the method heapify().

Step 2: swap the root with the last element of the array. Reduce the size of the heap by 1.

Step 3: Call the heapify() method again to maintain the property of max heap on remaining elements.

Step 4: Repeat steps 3 & 4 till the whole array gets sorted.

Pseudo Code

Java Program

The following Java program implements the heap sort using the pseudo-code explained above.

FileName: HeapSortExample.java

Output:

Explanation: The above program is imitating bubble sort. The heapify() method ensures that the maximum value element sits at the index 0. Then in one swap, the maximum element is put at the end of the array. When the heapify() method is called the second time, it puts the second maximum element at index 0.

The swapping process puts the second maximum element at the second last index of the array, and the process of invoking the heapify() method and swapping of elements continues till the whole array is sorted. The following diagram demonstrates the same.

Heap Sort in Java

Note that instead of using the max-heap, min-heap can also be used to sort. Using the min-heap, the heap sort resembles the selection sort as the root of the min-heap always gives the element of least value.

Analysis of the Heap Sort

Even though heap sort resembles the selection or bubble sort, the heap sort is much faster than the selection or bubble sort. However, similar to the selection or bubble sort, heap sort also divides the input array virtually into two halves; one is sorted, and another is unsorted.

Time Complexity

The heapify() method does the heapification process in O(log(n)) times, where n is the size of the input array. Also, the for-loop is iterating over each element of the array from right to left (see code). In each iteration, the heapify() method is invoked. Thus, for the for-loop time complexity is O(n) and O(log(n)) for the heapify() method. Hence, the total time complexity turns out to be O(n * log(n)) = O(nlog(n)). As O(nlog(n) is always less than O(n^2); therefore, heap sort is better than selection or bubble sort. One can think of the heap sorting algorithm as an improved version of selection sort or bubble sort.

Space Complexity

Similar to selection or bubble sort, heap sort algorithm also does the in-place sorting. The heap sort tries to visualize the input array as either min heap or max heap. Therefore, the space complexity for the heap sort turns out to be O(1), i.e., constant space for sorting the input array or list.

Conclusion

Heap sort works faster than bubble or selection sort. However, for larger data sets, it is found that merge sort is a better choice than heap sort, even though merge as well heap sort gives the time complexity of O(nlog(n)). Also, unlike merge sort, heap sort is not a stable sorting algorithm. However, by some modification, one can make merge sort stable. However, making heap sort stable increases the complexity of the algorithm.


Related Topics

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal. Using Integer.parseInt() method Using user-defined logic Using Integer.parseInt() method The parseInt() is a static method of...

2 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.