Java: InsertionSort

Another O(n²) sorting algorithm, however it’s more efficient than most O(n²) algorithms like BubbleSort and SelectionSort.

public static void insertionSort(int[] ia) {
	int l = ia.length;
	if (l == 1)
		return;
	for (int i = 0; i != l; i++)
		for (int j = i; j != 0; j--)
			if (ia[j] < ia[j - 1]) {
				int t = ia[j];
				ia[j] = ia[j - 1];
				ia[j - 1] = t;
			}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.