Insertion Sort in Java: Explained with Examples
Updated on Mar 28, 2025 | 7 min read | 5.7k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 7 min read | 5.7k views
Share:
Table of Contents
Sorting refers to the arrangement of elements in a specific order, often used in data structures such as arrays. The criteria in which the elements are sequenced is diverse. The most common ones are the sorting of numbers in ascending and descending order, lexicographical sorting of strings etc. Some of the most common and effective sorting algorithms used in Java and other object-oriented programming languages are:
Insertion sort is an easy-to-understand sorting algorithm whose working is similar to the manner we play cards. The input array list is split into two parts: sorted and unsorted. The splitting is virtual. The items from the unsorted part of the list are shifted to the sorted portion one at a time in each iteration.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
In insertion sort, the current element is compared with the immediate next element in each pass. The comparison is made to determine if the element is greater than the element it is compared to. If the item is greater than the one it is compared to, it is retained in the same position and the iteration continues. If the item is lesser than the one it is compared to, it is shifted to its appropriate position by shifting the larger items by one position backwards.
In the insertion sorting algorithm, the first iteration starts with an assumption that the item at the 0th position is already sorted. For each iteration, the current item is checked against the items in the sorted region of the list. The items that are greater during the comparison are moved towards the right of the list. In this algorithm, the current item can be inserted when the smaller item is reached.
Figure 1 shows an example of an insertion sort with a list of items. In the first iteration, the element [54] is assumed to be sorted already. This acts as a reference for the comparison during the first iteration. The second element [26] is less than 54 and hence, 54 is moved to the right.
Check out our free technology courses to get an edge over the competition.
Now that we have gained knowledge about the working of the insertion sort algorithm let’s visualize how a Java code is written to sort the elements with this algorithm. In the example code shown below, the values of arr can be changed as per your needs and how the program responds for different input arrays.
The output of the above code is mentioned below.
Explanation of the implemented Java code:
Check out upGrad’s Advanced Certification in DevOps
Insertion sort is an efficient sorting algorithm that allows for in-place sorting of an array.
It follows one element at a time. In this sorting approach, the array is changed without any temporary structures.
Below is the pseudocode for insertion sort in Java
INSERTION-SORT(array)
for i=2 to array.length
key_element = array[i]
j = i - 1
while j > 0 and array[j] > key_element
array[j+1] = array[j]
j = j - 1
array[j + 1] = key_element
Let’s quickly review the algorithm mentioned above.
In the recursive approach of Insertion Sort Java Code, the following differences from the iterative approach are acknowledged:
When the input sequence is in descending order, you wish to sort an array of numbers in ascending order. In this scenario, every element must be compared to every element to its left. This implies there are ‘n’ comparisons for each element after the first. There are hence ‘n(n-1)’ comparisons in total. Hence, the worst-case complexity for an input size of n is O(n2).
When the array is already sorted, the outer loop runs n times, and the inner loop is never called. Therefore, the best-case complexity for an input size of ‘n’ is O(n).
The average case complexity for an input size of ‘n’, when the array components are arranged randomly, is O(n2).
At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources