I have created an Insertion Sort code that sorts numbers (obviously).#include <iostream>#include <cstdlib>void insertion_sort(int arr[], int length);void print_array(int array[],int size);int main(){    int array[] = {4,6,3,7,5,9,2,8,1,10};    insertion_sort(array,10);    print_array(array,10);    return 0;}void insertion_sort(int arr[], int length){    int i,j,tmp;    for (i = 1; i < length; i++) {        j = i;        while (j > 0 && arr[j - 1] > arr[j]) {            tmp = arr[j];            arr[j] = arr[j - 1];            arr[j - 1] = tmp;            j--;        }    }}void print_array(int array[], int size){    int j;    for (j = 0; j < size; j++)    {        std::cout << array[j] << std::endl;    }}Is there a way to make it shorter or better?
Insertion Sort in C++
c++;c++11;insertion sort
A couple of things:int i,j,tmp;can be formatted better:int i, j, tmp;Even that would be discouraged, in two ways:Declare variables where you need them, not before.Multiple declarations on one line isn't very readable.and actually isn't even necessary, as is explained later.int j;for (j = 0; j < size; j++){    std::cout << array[j] << std::endl;}C++ conventions have braces on the same line as the statement:int j;for (j = 0; j < size; j++) {    std::cout << array[j] << std::endl;}In fact, the first line can be merged into the loop:for (int j = 0; j < size; j++) {    std::cout << array[j] << std::endl;}j = i;while (j > 0 && arr[j - 1] > arr[j]) {    tmp = arr[j];    arr[j] = arr[j - 1];    arr[j - 1] = tmp;    j--;}can be a for loop, as explained:    j = i; // Initialization    while (j > 0 && arr[j - 1] > arr[j]) { // Condition        tmp = arr[j];        arr[j] = arr[j - 1];        arr[j - 1] = tmp;        j--; // Decrement    }which turns into:for (j = i; j > 0 && arr[j - 1] > arr[j]; j--) {    tmp = arr[j];    arr[j] = arr[j - 1];    arr[j - 1] = tmp;}Now, why is the first line not necessary, and even discouraged?Well, because you should declare variable right where you need them, and not before:void insertion_sort(int arr[], int length){    for (int i = 1; i < length; i++) {        for (int j = i; j > 0 && arr[j - 1] > arr[j]; j--) {            int tmp = arr[j];            arr[j] = arr[j - 1];            arr[j - 1] = tmp;        }    }}