
Idea: Insert each element into its correct position
O(n) (nearly sorted)O(n²)When to use
public class InsertionSort {
public static void sort(int[] a) {
for (int i = 1; i < a.length; i++) {
int key = a[i];
int j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
}
Watch for
Mental model: š Insert into correct position