Insertion Sort - Part 2

  • + 1 comment

    Hi, I've written a similar code but it failed 3/5 test cases reason being error with ArrayOutOfBound exception. Can you please tell me what wrong did I do? Thanks.

    for(int j=1;j<ar.length;j++){
                    int temp=ar[j];
                    int k = j-1;
                    while(ar[k]>temp&&k>=0)
                    {
                        ar[k+1]=ar[k];
                        k--;
                    }
                    ar[k+1]=temp;
                    printArray(ar);
                }