StackTips
 2 minutes

Insertion sort example in Java

By Editorial @stacktips, On Sep 17, 2023 Java 2.31K Views

Code snippet to sort array using insertion sort algorithm in java.

public class InsertionSortExample {
	static int step = 1;

	public static void main(String[] args) {

		int[] array = { 17, 21, 191, 3, 23, 45, 34, 9, 1 };

		int n = array.length;
		for (int j = 1; j < n; j++) {
			int key = array[j];
			int i = j - 1;
			while ((i > -1) && (array[i] > key)) {
				array[i + 1] = array[i];
				i--;
			}
			array[i + 1] = key;
			printNumbers(array);
			
			System.out.println("n");
		}
	}

	private static void printNumbers(int[] input) {
		System.out.println("--- step " + step + " ----");
		step++;

		for (int i = 0; i < input.length; i++) {
			System.out.print(input[i] + ", ");
		}
	}
}

Output
Insertion sort example in Java

stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.