
5.6K
MADay 2/30 | DSA | Insertion Sort
How it works:
Start from the second element.
Compare it with elements before it.
Shift larger elements one position to the right.
Insert the current element into its correct position.
Efficient for small datasets or nearly sorted arrays.
Code (Java):
void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
Follow @masternora for more
Likes
#coding #programming #java #algorithm #dsa #sorting #quicksort #programmers #coders #dev #viralreels #trendingreels #trending #viral #instaindia #instagood #instagram #codewithbrain #cse #dsa #datastructures #insertionsort
@masternora










