Introduction
Sorting algorithms are an essential part of computer science, and one of the simplest among them is the Insertion Sort. Though it’s not the fastest for large datasets, its ease of implementation make it a great starting point for understanding how sorting works.
What is Insertion Sort?
Insertion Sort works by gradually building a sorted section of an array. As it moves through the list, it picks one element at a time and places it where it belongs among the already sorted elements. Think of how you’d sort a hand of cards—you pick up one card, then place it in the correct position relative to the cards already in your hand.
This algorithm is in-place, meaning it doesn’t require extra memory, and stable, so equal elements retain their original order.
Insertion Sort Logic
The process involves:
- Starting from the second element of the array.
- Comparing it to the elements before it.
- Shifting those greater than it one position to the right.
- Inserting the current element into the proper location.
- Continuing this until the entire array is sorted.
When the array is already sorted, Insertion Sort runs efficiently with a time complexity of O(n). But in the worst-case scenario—like when the array is sorted in reverse—it slows down to O(n²).
Insertion Sort Java Implementation
Here’s a simple implementation of the algorithm in Java:
@Test
public void BubbleSortTest() throws Exception {
int[] arrToBeSorted = {55, 66, 88, 99, 677, 55, 25, 77};
bubbleSortingAlg(arrToBeSorted);
for (int elem : arrToBeSorted) {
System.out.println(elem + " ");
}
}
private void bubbleSortingAlg(int[] arrToBeSorted) {
int n = arrToBeSorted.length;
boolean swappedElems;
for (int i = 0; i < n - 1; i++) {
swappedElems = false;
for (int j = 0; j < n - i - 1; j++) {
if (arrToBeSorted[j] > arrToBeSorted[j + 1]) {
int temp = arrToBeSorted[j];
arrToBeSorted[j] = arrToBeSorted[j + 1];
arrToBeSorted[j + 1] = temp;
swappedElems = true;
}
}
if (!swappedElems) {
break;
}
}
}
In this code, the insertionSort method loops through the array, moving each element into its appropriate place among the previously sorted values. The while loop handles the shifting of elements until the right spot is found for the current item.
Insertion Sort Use Cases
This method of sorting is ideal when dealing with small datasets or lists that are already almost sorted. It’s not efficient for large arrays due to its O(n²) time complexity in most cases, but it’s still commonly used in practice for short lists and as a building block in more complex algorithms.
Conclusion
Insertion Sort may not be the fastest, but it’s an important stepping stone for anyone learning about algorithms. Its logic is easy to follow, the code is short and readable, and it introduces core ideas like element comparison, in-place sorting, and algorithmic efficiency. is easy to follow, the code is short and readable, and it introduces core ideas like element comparison, in-place sorting, and algorithmic efficiency.
You can find the example code on Github.
