Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Insertion Sort

1 topic · 28 Nov 2026
1

Insertion Sort

Algorithm › Sort

Take the next element and insert it into its correct place in the already-sorted left part, like sorting playing cards. Very fast for small or nearly-sorted arrays.

C++
void insertionSort(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i];
        int j = i - 1;
        while (j >= 0 && a[j] > key) {
            a[j + 1] = a[j];   // shift right
            j--;
        }
        a[j + 1] = key;
    }
}