Introduction to Insertion Sort

Insertion Sort is a simple comparison-based sorting algorithm. It is also called an in-place sorting algorithm because it sorts elements in an array or a list by moving the elements one by one and inserting them at their correct position. It's efficient for small data sets or partially sorted data.
Insertion Sort is a sorting algorithm that builds the final sorted array one item at a time. The array or list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part consists of only the first element. With each iteration, one element from the unsorted portion is taken and placed at its correct position in the sorted portion of the array.
How does it work?
The Insertion Sort algorithm consists of the following steps:
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick the next element.
Step 3 − Compare with all the elements in the sorted sub-list.
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted.
Step 5 − Insert the value.
Step 6 − Repeat until the list is sorted.
Implementation
Here's an implementation of the insertion sort algorithm in Java :
static void sort(int [] arr){
for(int i=0; i<arr.length-2; i++){
for(int j = i+1; j>0; j--){
if(arr[j]<arr[j-1]){
int temp= arr[j];
arr[j]= arr[j-1];
arr[j-1]= temp;
}
else{
break;
}
}
}
}
Here's an example of insertion sort:
arr= [9, 14, 3, 2, 43, 11, 58, 22]
//1st Pass: (compares the value of j and j-1 and found that j is greater than j-1 and the moves ahead)
[9, 14, 3, 2, 43, 11, 58, 22]
// 2nd Pass: (compares the value of and j-1 and find that j-1 is greater so replace that and decrement the vlue of j, after that it again checks that is j greater than j-1 if not then then replacee then)
[3, 9, 14, 2, 43, 11, 58, 22]
//3rd Pass:
[2, 3, 9, 14, 43, 11, 58, 22]
//4th Pass:
[2, 3, 9, 14, 43, 11, 58, 22]
//5th Pass:
[2, 3, 9, 11, 14, 43, 58, 22]
//6th Pass:
[2, 3, 9, 11, 14, 43, 58, 22]
//7th Pass:
[2, 3, 9, 11, 14, 22, 43, 58]
Output array: [2, 3, 9, 11, 14, 22, 43, 58]
Conclusion:-
As we can see from the example above, the insertion sort algorithm starts with the first two elements and compares them. If the second element is smaller than the first one, it is swapped. Then the algorithm moves to the next element and compares with its preceding element until the last element is reached.
In this implementation, we take the initial element and compare it with the preceding elements until we find its correct position. Finally, we prints the sorted array.
For more such blogs on Java programming language and sorting algorithms stay updated.
About the author
I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .
Comments
Join the discussionNo comments yet. Be the first to comment.




