Array Insertion Sort

in this we are doing j-- in while loop but when we do j-- in first interation the value of j becomes -1 when and it checks in while arr[-1] which give array out of bount exceptionm.

You should give condition in while for j>-1 && arr[j]>val

Hi Vaibhav

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

In this code, we are doing j-- at the end of the loop. So before that, the value of j is 0 which is a valid index.