Homework 4: Insertion SOrt Algorithm

Insertion sort goes through the array one element at a time. It takes the current element and places it in the correct position among the already sorted elements before it.

Difficulty:
★★★☆☆
#include 

int main()
{
    int arr[] = {9, 5, 1, 4, 3};
    int n = 5;
    int i, j, key;

    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;

        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }

        arr[j + 1] = key;
    }

    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;
}