Insertion Sort is a sorting algorithm that picks and places one element at a time in its right position by comparing and shifting them.

iKey - Pick an element from the array which is at index ij will be i-1j>=0 and array element at j>keyj+1#include <stdio.h>
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
void printArray(int arr[], int n){
for (int i = 0; i < n; i++){
printf("%d ", arr[i]);
}
}
void insertitionSort(int arr[], int n){
for (int i = 1; i < n; i++){
int key = arr[i]; // Value to be place at right position
int j = i - 1; // J for left side comparing array left side of temp
while (j >= 0 && arr[j] > key){
arr[j + 1] = arr[j]; // Shifting array one step right
j--;
}
arr[j + 1] = key; // Placing element to right position
}
}
void main(){
int arr[] = {5,4,2,3,1};
int n=5;
insertitionSort(arr, n);
printArray(arr, 5);
}