Insertion Sort Example in Java
On this page (9sections)
Introduction
Insertion Sort is a classic Java console program that demonstrates the concept with complete source code and sample output. Sorting algorithms arrange data in order — bubble, selection, merge, quick and heap sort.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages like simple implementation, efficient for (quite) small data sets, more efficient in practice than most other simple quadratic algorithms, adaptive, stable, in-place; i.e., only requires a constant amount of additional memory space, online; i.e., can sort a list as it receives it
Insertion Sort Example Program
import java.util.Arrays;
public class InsertionSort {
public static void main(String[] args) {
int arr1[] = new int[10];
populateArray(arr1);
System.out.println("Numbers to be sorted: ");
printArray(arr1);
insertionSort(arr1);
System.out.println("\nAfter Sorting: ");
printArray(arr1);
}
private static void insertionSort(int[] arr2) {
for (int i = 1; i < arr2.length; i++) {
int sort = arr2[i];
int j = i;
while (j > 0 && arr2[j - 1] > sort) {
arr2[j] = arr2[j - 1];
j--;
}
arr2[j] = sort;
}
}
public static void printArray(int[] arr3) {
System.out.println(Arrays.toString(arr3));
}
public static void populateArray(int[] arr3) {
for (int i = 0; i < arr3.length; i++) {
arr3[i] = (int) (Math.random() * 100);
}
}
}
Sample Output
Numbers to be sorted:
[29, 6, 25, 7, 15, 86, 48, 78, 29, 4]
After Sorting:
[4, 6, 7, 15, 25, 29, 29, 48, 78, 86]
When to use
Use this insertion sort example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.util.Arrays;imports a class used later in the program. -
int arr1[] = new int[10];updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
for (int i = 1; i < arr2.length; i++) {updates a variable used in the calculation or output. -
int sort = arr2[i];updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.