# Write a program to Sort Array using Insertion Sort.

#include<stdio.h>
#define size 10
int main()
{
int i,j,temp;
int A[size]={5,6,8,9,0,2,3,4,1,7};

for(j=1;j<size;j++)
{
temp=A[j];
i=j-1;
while(i>-1 && A[i]>temp)
{
A[i+1]=A[i];
i=i-1;
}
A[i+1]=temp;
}
printf(“The sorted Array is: “);
for(i=0;i<size;i++)
{
printf(“%d\n”,A[i]);
}
}
Explanation of program.
#define size 10 is used for define a constant .. its value will remain same through out the program.
the value of constant can not be changed even we want.

int A[size]={5,6,8,9,0,2,3,4,1,7}; is used for declare an array and assigning values to array.

for(j=1;j<size;j++) is used for sorting the array and we use j=1 but not j=0 or j=5 because the
elements of array we can assume that they are sorted only 1. and that is on location 0.

temp=A[j]; save the value of j index of array in temp.

i=j-1; because we was started the value of j from 1. while now we want to start the inner loop
from zero.therefor i=j-1

in while loop condition we are seeing that i is greater than -1(means that array starting from zero)
and A[i]>temp means that the value at the i index of Array is greater than temp or not.
in temp we were have the previous element

A[i+1]=A[i]; replacing the value of next index with current index.
i=i-1; is decrement in i.

A[i+1]=temp; replace the value of A[i+1] with temp which we was stored of previous element.

for(i=0;i<size;i++) the 2nd for loop is used for printing the array from 0 index to size

printf(“%d\n”,A[i]); prints the element at cuurent location. i representing the location of array.
*/