×

Sorting Algorithms in Java

Sorting Algorithms in Java

Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements are {x1, x2, x3, … xn}.

The array A is said sorted when its elements either follow ascending order, i.e., x1 > x2 > x3 > … > xn, or descending order, i.e., x1 < x2 < x3 < … < xn. The sorting algorithms in Java talks about various algorithms that can be used for sorting the elements of the array or list.

Different algorithms of sorting

The following table demonstrates the different sorting algorithms that are used data structure.

Sorting AlgorithmsDescription
Merge SortIt works on the principle of divide and conquer. This algorithm divides the given list into two halves of equal lengths. Those halves are recursively divided into further equal halves. These halves are sorted and then merge, ultimately resulting in the sorted list.
Quick SortIt is a type of comparison sort which also follows the divide and conquer approach. In this algorithm, elements are chosen as the pivot, and then sorting is done.
Bubble SortBubble sort is one of the simplest sorting algorithms that swaps adjacent elements of the given list till the array is sorted. Sometimes the bubble sort is also known as the sinking sort.
Insertion SortIt puts an unsorted element at its appropriate position in each iteration, similar to the arrange of playing cards.
Selection SortIt finds the smallest, second smallest, third smallest element, and so on in each iteration. Selection sort is not commonly used because better algorithms such as merge insertion or quick sort are present.
Heap SortHeap sort uses the binary heap for doing the sorting. For sorting in ascending order, a min-heap is used. For sorting in descending order, a max-heap is used.
Radix SortRadix sort is also known as the non-comparative sorting algorithm. This is because, unlike other sorting algorithms, it does not do the sorting of elements. In this sorting algorithm, sorting is done on the basis of digits of the number, moving from LSD (Least Significant Digit) to MSD (Most Significant Digit) of the number present in the list.
Topological SortIt is used in a directed acyclic graph to put the linear style of the ordering of elements, where for every directed edge like x -> y, x comes before y. Topological sorting can be achieved by DFS (Depth First Search) as well as BFS (Breadth-First Search).
Bucket SortIn this type of sorting, an element of the given list is put into a number of buckets. Each bucket is then sorted using different algorithms. In the end, the concatenation of the buckets gives the sorted list.
Counting SortTo sort numbers between a given range provided the range is small, counting sort is used. Counting sort does not make any comparison between the elements of the list; hence it works very fast. In counting sort, the rank of the elements is found to do the sorting.

We will discuss each sorting algorithm in the coming sections.


Related Topics

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Block Swap Algorithm for array rotation in Java

An array and r, the rotation factor by which the array must be rotated, are both provided to us. We must then return the rotated array. A well-known and popular method is...

4 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

How to Convert Decimal to Binary in Java

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

2 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.